节约问题&打开一个BitMap文件

时间:2011-07-22 20:16:36

标签: android

在这段代码中,我试图:

  1. 从ImageView获取位图
  2. 将其另存为“er.png”
  3. 然后加载它并将其分配给另一个ImageView
  4. 这是我的代码......但是它一直在触发它无法写入的异常...任何想法可能导致它?也许它需要一些权限?

    ////// GET FROM IV /////////////////////////////////////
            ImageView iv = (ImageView)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));
    

    谢谢!

3 个答案:

答案 0 :(得分:4)

尝试添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

到清单

答案 1 :(得分:2)

您的清单中是否有权限WRITE_EXTERNAL_STORAGE?

答案 2 :(得分:1)

尝试在清单中添加此内容:uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”