在Lazy-List中,我使用此代码,并在名为LazyList的文件夹中存储图像
ImageLoader imageLoader=new ImageLoader(context); imageLoader.DisplayImage(url, imageView);
但是在Universal-Image-Loader中我使用这个
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.build();
ImageLoader.getInstance().init(config);
img = (ImageView) this.findViewById(R.id.test_id);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(URL.RECIPE_IMG_URL,img);但是,每次它使用互联网获取图像,并且没有将我的图像存储在任何文件夹中时,我都会将.diskCache(new UnlimitedDiscCache(new File("myFolder")))添加到config中,而不会在myFolder.Any ideas中存储任何内容。
发布于 2014-08-19 05:01:59
我找到了答案。默认情况下,缓存是禁用的,我应该将DisplayImageOptions添加到ImageLoaderConfiguration
这是代码
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();发布于 2014-08-18 17:56:40
您必须将图像保存在onLoadingComplete上
在onLoadingComplete上,您必须将bitmap保存到变量bitmap
imageLoader.getInstance().displayImage(imagePath, imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
showedImgae = loadedImage;
}
});现在单击保存按钮/如果您想保存Bitmap/Image以保存SDCard,请使用以下命令
public void downloadImage(){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/DCIM/youfoldername");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "imagename-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
Toast.makeText(MyActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
getApplicationContext().sendBroadcast(mediaScanIntent);
}希望它能帮到你..。
https://stackoverflow.com/questions/25367727
复制相似问题