我需要保存来自网络服务提供的网址的图片,然后在某些活动中我需要检索并使用该图片。
由于我已经使用Picasso下载和缓存图像,我使用了以下方法:
public void downloadFile(String url) {
Picasso.with(AnimationActivity.this).load( url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/bBackground");
if (!myDir.exists()) {
myDir.mkdirs();
}
String name = "bBg.jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch(Exception e){}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
});
}
然后为了检索我尝试使用以下代码的图像:
String bgImagePath = Environment.getExternalStorageDirectory() + "/bBackground/bBg.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(bgImagePath, options);
ImageView imageView = (ImageView) findViewById(R.id.bBgImage);
imageView.setImageBitmap(mustOpen);
但是我在检索图片时遇到错误:
E/BitmapFactory(30761): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/bBackground/bBg.jpg: open failed: ENOENT (No such file or directory)
我做错了什么?这是与保存或检索方法有关的问题吗?
答案 0 :(得分:2)
您是否在清单文件中添加了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
要保存图像,请使用以下功能:
public void saveBitmap(Bitmap bitmap) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/bBackground");
if (!dir.exists()) {
dir.mkdirs();
}
File image = new File(dir, "/bBg.jpg");
try {
FileOutputStream out = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
}
要阅读此图片,您可以使用:
public Bitmap getBitmap(){
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/bBackground");
File image = new File(dir, "/bBg.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(image, options);
ImageView imageView = (ImageView) findViewById(R.id.bofrostBgImage);
imageView.setImageBitmap(mustOpen);
}
答案 1 :(得分:0)
尝试此代码此问题与picasso目标有关。
private Target loadtarget;
public void loadBitmap(String url) {
if (loadtarget == null)
loadtarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap,
Picasso.LoadedFrom from) {
// do something with the Bitmap
handleLoadedBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
};
Picasso.with(this).load(url).into(loadtarget);
}
public void handleLoadedBitmap(Bitmap bitmap) {
// do something here
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/bBackground");
if (!myDir.exists()) {
myDir.mkdirs();
}
String name = "bBg.jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String bgImagePath = Environment.getExternalStorageDirectory()
+ "/bBackground/bBg.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(bgImagePath, options);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(mustOpen);
}