problems saving & opening a BitMap file
In this code I'm trying to:
- get a bitmap from an ImageView
- save it as "er.png"
- then load it and assign it to another ImageView
here's my code... but it keeps firing the exception that it can't write... any ideas what might be causing it? Maybe it needs some permissions?
////// GET FROM IV /////////////////////////////////////
ImageView iv = (ImageView)开发者_JAVA技巧findViewById(R.id.ivDon);
Drawable draw = iv.getBackground();
Bitmap bm = ((BitmapDrawable)draw).getBitmap();
Bitmap bmHue = Hue(bm,180);
//////////////////////////// SAVE ///////////
Bitmap bbicon = bmHue;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, "er.PNG");
try {
outStream = new FileOutputStream(file);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
}
catch(Exception e)
{Toast.makeText(this, "error writing :/", 5000).show();} // <- this exception fires.
///////////////////////////// LOAD //////////////
java.io.FileInputStream in = null;
try {
in = openFileInput("er.PNG");
} catch (FileNotFoundException e)
{Toast.makeText(this, "error reading :/", 5000).show();}
////////////////////////////// ASSIGN ///////////////
ImageView iv1 = (ImageView)findViewById(R.id.iv01);
iv1.setImageBitmap(BitmapFactory.decodeStream(in));
Thanks!
Try adding:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to the manifest
Do you have the permission WRITE_EXTERNAL_STORAGE in your manifest?
Try add this in the manifest: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
精彩评论