我是Android新手。在我的应用程序中,我有一个带有图像的列表视图。我想从url下载图像并存储在路径中(如果图像不存在于路径中)。然后在pathView中设置图像。我做了,但有些问题就在那里。
我的问题:
首次下载时,它会在ListView的每个项目中逐一显示所有图像。最后,设置它设置完美的图像。
从路径设置图像意味着设置图像需要很长时间。
请任何人解决我的问题。
这是我的代码:
我的适配器代码:
if (ImageStorage.checkifImageExists(ImageStorage.getImageName(list.get(position).imageUrl))) {
File file = ImageStorage.getImage("/" + ImageStorage.getImageName(list.get(position).imageUrl));
String path = file.getAbsolutePath();
Picasso.with(context)
.load(new File(path))
.placeholder(R.drawable.loading)
.error(R.drawable.error)
.into(holder.menuImage);
} else {
new ImageDownloaderTask(list.get(position).imageUrl, holder.menuImage, ImageStorage.getImageName(list.get(position).imageUrl)).execute();
}
ImageDownloaderTask.java:
public class ImageDownloaderTask extends AsyncTask<Object, Object, Object> {
private String requestUrl, imagename_;
private ImageView view;
private Bitmap bitmap ;
private FileOutputStream fos;
public ImageDownloaderTask(String requestUrl, ImageView view, String _imagename_) {
this.requestUrl = requestUrl;
this.view = view;
this.imagename_ = _imagename_ ;
}
@Override
protected Object doInBackground(Object... objects) {
try {
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
InputStream is = new URL( requestUrl ).openStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (Exception ex) {
}
return null;
}
@Override
protected void onPostExecute(Object o) {
if(!ImageStorage.checkifImageExists(imagename_))
{
view.setImageBitmap(bitmap);
ImageStorage.saveToSdCard(bitmap, imagename_);
}
}
}
ImageStorage.java:
public class ImageStorage {
public static String saveToSdCard(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File folder = new File(sdcard.getAbsoluteFile(), "tam");//the dot makes this directory hidden to the user
folder.mkdir();
File file = new File(folder.getAbsoluteFile(), filename) ;
if (file.exists())
return stored ;
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
stored = "success";
} catch (Exception e) {
e.printStackTrace();
}
return stored;
}
public static File getImage(String imagename) {
File mediaImage = null;
try {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDir = new File(root);
if (!myDir.exists())
return null;
mediaImage = new File(myDir.getPath() + "/tam/"+imagename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mediaImage;
}
public static boolean checkifImageExists(String imagename)
{
Bitmap b = null ;
File file = ImageStorage.getImage("/"+imagename);
String path = file.getAbsolutePath();
if (path != null)
b = BitmapFactory.decodeFile(path);
if(b == null || b.equals(""))
{
return false ;
}
return true ;
}
public static String getImageName(String value){
String getName[] = value.split("/");
Log.d("ImagesName",getName[4]);
return getName[4];
}
}
答案 0 :(得分:0)
您不必创建位图只是检查它是否存在,您只能通过文件检查。
public static boolean checkifImageExists(String imagename) {
File file = ImageStorage.getImage("/" + imagename);
if (file.exists()) {
return true;
} else {
return false;
}
}
更新部分:
为了更快地执行速度,您可以添加修改
public static boolean checkifImageExists(String imagename)
到
public static File checkifImageExists(String imagename)
如果文件存在则返回文件,否则为null然后你不必再做这一步。
public static File checkifImageExists(String imagename) {
File file = ImageStorage.getImage("/" + imagename);
if (file.exists()) {
return file;
} else {
return null;
}
}
在适配器中制作一些chagne。
File file = ImageStorage.checkifImageExists(ImageStorage.getImageName(list.get(position).imageUrl));
if (file != null) {
// this step skips
//File file = ImageStorage.getImage("/" + ImageStorage.getImageName(list.get(position).imageUrl));
// and this one also skips
//String path = file.getAbsolutePath();
Picasso.with(context)
.load(file) // why creating new file if you already have same file.
.placeholder(R.drawable.loading)
.error(R.drawable.error)
.into(holder.menuImage);
} else {
new ImageDownloaderTask(list.get(position).imageUrl, holder.menuImage, ImageStorage.getImageName(list.get(position).imageUrl)).execute();
}
<强> onPostExecute()强>
@Override
protected void onPostExecute(Object o) {
// Dont check here you dont have image then you are downloading it.its Already checked?
// rather then check bitmap you are going to save should not be null
if (bitmap != null) {
view.setImageBitmap(bitmap);
ImageStorage.saveToSdCard(bitmap, imagename_);
}
}