I need to save an image from an Url provided by a webservice and then in some Activities I need to retrieve and use that image.
Since I was already using Picasso in order to download and cache images I've used the following method:
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) {}
});
}
And then in order to retrieve the image I've tried with the following code:
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);
But I get an error when retrieving the image:
E/BitmapFactory(30761): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/bBackground/bBg.jpg: open failed: ENOENT (No such file or directory)
What am I doing wrong? Is it a problem related to the saving or retrieving method?
Have you added following permission in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
To save image use following function:
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();
}
}
To read this image you can use:
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);
}
try this code this issue is with picasso target.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.