开发者

problems saving & opening a BitMap file

In this code I'm trying to:

  1. get a bitmap from an ImageView
  2. save it as "er.png"
  3. 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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜