从 assets 文件夹中获取图像并在 android 中填充 gridview
Fetch Images from assets folder and fill gridview in android
我正在开发一个小应用程序,我对 android 知之甚少。所以我需要你的帮助。实际上我把一些大量的图像放在资产子文件夹中。现在我想要的是,我想阅读我放入资产子文件夹中的所有图像,例如 "images" 文件夹,然后我想在网格视图中填充所有图像。请朋友们帮我在网格视图中填充图像。
我曾尝试关注 link 但我对这个网站并不满意
Loading Images from assets to GridView with smooth Scrolling.
我实际上在 https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
上找到了这个答案
public void loadFromAsset() {
InputStream is = getAssets().open("filename.extension");
//Load image as drawable
Drawable draw = Drawable.createFromStream(is, null);
//Once you get the drawable, set the image to imageView
imageView.setImageDrawable(draw);
}
然后将下面的代码粘贴到 CustomAdapter GridViewAdapter
public class GridViewAdapter 扩展了 BaseAdapter {
Context context;
int layoutResourceId;
private ArrayList<String> griRowItems;
LayoutInflater vi;
ImageLoader imageLoader;
String[] imageUrls;
ArrayList<String> imagePath;
DisplayImageOptions options;
ViewHolder holder = null;
public GridViewAdapter(Context context,
int layoutResourceId, ArrayList<String> mApps) {
this.context = context;
this.griRowItems = mApps;
this.layoutResourceId = layoutResourceId;
imageLoader = ImageLoader.getInstance();
this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
vi = (LayoutInflater) context
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = vi.inflate(R.layout.grid_double, parent,
false);
holder = new ViewHolder();
}
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
// holder.progressBar = (ProgressBar) convertView
// .findViewById(R.id.progress);
String tempStr = "assets://" + griRowItems.get(position);
lazyLoading(imageLoader, tempStr, holder.imageView,
options);
return convertView;
}
@Override
public int getCount() {
return griRowItems.size();
}
@Override
public Object getItem(int position) {
return griRowItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static void lazyLoading(ImageLoader imageLoader, String tempStr, ImageView imageView,DisplayImageOptions options) {
imageLoader.displayImage(tempStr, imageView, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view,int current, int total) {
}
});
}
static class ViewHolder {
ImageView imageView;
// ProgressBar progressBar;
//TextView tvTitle;
// DisplayImageOptions options;
}
public class Flower_Mahendi 扩展 Activity {
ArrayList<String> listPath;
GridViewAdapter adp;// = new GridViewAdapter(null, 0, listPath);
GridView gridView;
//Bitmap listBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flower_mahendi);
AssetManager assetManager = getResources().getAssets();
gridView = (GridView) findViewById(R.id.gridView1);
try {
String[] files = assetManager.list("kids");
listPath = new ArrayList<String>();
for (String strImageName : files) {
String pathAssets = "kids" + File.separator
+ strImageName;
listPath.add(pathAssets);
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if (listPath!= null) {
adp = new GridViewAdapter(this,R.layout.grid_double, listPath);
gridView.setAdapter(adp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我正在开发一个小应用程序,我对 android 知之甚少。所以我需要你的帮助。实际上我把一些大量的图像放在资产子文件夹中。现在我想要的是,我想阅读我放入资产子文件夹中的所有图像,例如 "images" 文件夹,然后我想在网格视图中填充所有图像。请朋友们帮我在网格视图中填充图像。 我曾尝试关注 link 但我对这个网站并不满意 Loading Images from assets to GridView with smooth Scrolling.
我实际上在 https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
上找到了这个答案public void loadFromAsset() {
InputStream is = getAssets().open("filename.extension");
//Load image as drawable
Drawable draw = Drawable.createFromStream(is, null);
//Once you get the drawable, set the image to imageView
imageView.setImageDrawable(draw);
}
然后将下面的代码粘贴到 CustomAdapter GridViewAdapter
public class GridViewAdapter 扩展了 BaseAdapter {
Context context;
int layoutResourceId;
private ArrayList<String> griRowItems;
LayoutInflater vi;
ImageLoader imageLoader;
String[] imageUrls;
ArrayList<String> imagePath;
DisplayImageOptions options;
ViewHolder holder = null;
public GridViewAdapter(Context context,
int layoutResourceId, ArrayList<String> mApps) {
this.context = context;
this.griRowItems = mApps;
this.layoutResourceId = layoutResourceId;
imageLoader = ImageLoader.getInstance();
this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
vi = (LayoutInflater) context
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = vi.inflate(R.layout.grid_double, parent,
false);
holder = new ViewHolder();
}
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
// holder.progressBar = (ProgressBar) convertView
// .findViewById(R.id.progress);
String tempStr = "assets://" + griRowItems.get(position);
lazyLoading(imageLoader, tempStr, holder.imageView,
options);
return convertView;
}
@Override
public int getCount() {
return griRowItems.size();
}
@Override
public Object getItem(int position) {
return griRowItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static void lazyLoading(ImageLoader imageLoader, String tempStr, ImageView imageView,DisplayImageOptions options) {
imageLoader.displayImage(tempStr, imageView, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view,int current, int total) {
}
});
}
static class ViewHolder {
ImageView imageView;
// ProgressBar progressBar;
//TextView tvTitle;
// DisplayImageOptions options;
}
public class Flower_Mahendi 扩展 Activity {
ArrayList<String> listPath;
GridViewAdapter adp;// = new GridViewAdapter(null, 0, listPath);
GridView gridView;
//Bitmap listBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flower_mahendi);
AssetManager assetManager = getResources().getAssets();
gridView = (GridView) findViewById(R.id.gridView1);
try {
String[] files = assetManager.list("kids");
listPath = new ArrayList<String>();
for (String strImageName : files) {
String pathAssets = "kids" + File.separator
+ strImageName;
listPath.add(pathAssets);
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if (listPath!= null) {
adp = new GridViewAdapter(this,R.layout.grid_double, listPath);
gridView.setAdapter(adp);
}
} catch (Exception e) {
e.printStackTrace();
}
}