2015-04-02 153 views
1

我想要从网络保存图像在内部文件夹(数据/数据/ package_name/...),但我发现异常时,我打电话Bitmap.compress()方法。请帮我找到我的错误。 这是我的代码:不工作bitmap.compress

FileOutputStream out = null; 
    try { 
     File fileDir = new File(PATH_FOR_IMAGES); 
     if (!fileDir.exists()) { 
      fileDir.mkdirs(); 
     } 
     File file = new File(PATH_FOR_IMAGES + nameOfBitmap); 
     if (!file.exists()) { 
      file.createNewFile(); 
     } 
     out = new FileOutputStream(file); 
     final FileOutputStream finalOut = out; 
     final FileOutputStream finalOut1 = out; 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, finalOut); 
       try { 
        if (finalOut1 != null) { 
         finalOut1.close(); 
        } 
       } catch (Exception e) { 
        throw new RuntimeException(); 
       } 
      } 
     }).start(); 
    } catch (Exception e) { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (Exception e1) {} 
     throw new RuntimeException(); 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (Exception e) {} 
    } 

这没有任何权限的清单:

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

而这个错误printStackTrace()日志中:

D/skia﹕ ------- write threw an exception 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ java.io.IOException: write failed: EBADF (Bad file number) 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:462) 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.io.FileOutputStream.write(FileOutputStream.java:187) 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.nativeCompress(Native Method) 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.compress(Bitmap.java:904) 
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at com.DriverNotes.AndroidMobileClient.extra.SponsorManager$1.run(SponsorManager.java:330) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.lang.Thread.run(Thread.java:856) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.writeBytes(Native Method) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.write(Posix.java:187) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:457) 
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ ... 5 more 
+0

你的代码是尝试和catch块与在随机线程的混乱中间。我不惊讶,它不起作用。坦率地说,我不太确定你是否理解同步VS异步代码的概念。另外很可能你的代码会造成内存泄漏。 – 2015-04-02 09:14:34

回答

1

试试这个代码

位图relativeBitmap;

公共无效saveFile的(){

 View view=(RelativeLayout)findViewById(R.id.relativeLayout); 
     relativeBitmap= takeSnapshot(view); 
     OutputStream output=null; 
     //Find the SD Card path 
     File filePath = Environment.getExternalStorageDirectory(); 
     // Create a new folder in SD Card 
     File dir = new File(filePath.getAbsolutePath()+ "/Save Image Frame/"); 
     if(!dir.exists()) 
      dir.mkdirs(); 

     // Create a name for the saved image 
     File file = (File) new File(dir, "myimage.png"); 

     // Show a toast message on successful save 
     Toast.makeText(MainActivity.this, "Image Saved to SD Card", 
     Toast.LENGTH_SHORT).show(); 

     try 
     { 
      output=new FileOutputStream(file); 
       // Compress into png format image from 0% - 100% 
      relativeBitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
      output.flush(); 
      output.close(); 


      } 
      catch (FileNotFoundException e) 
      { 

       e.printStackTrace(); 

      } 
      catch (IOException e) 
      { 

       e.printStackTrace(); 

      } 
      catch (Exception e) 
      { 

       e.printStackTrace(); 

      } 

     } 

//的onClick中保存此方法将被称为

private Bitmap takeSnapshot(View v) 
    { 
       if(bitmap!=null) 
       { 
        bitmap.recycle(); 
        bitmap=null; 
       } 
       bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),Bitmap.Config.ARGB_8888); 
       Canvas c = new Canvas(bitmap); 
              /*c.drawBitmap(v.getDrawingCache(), 0, 0, null); 
              v.destroyDrawingCache();*/ 
       v.draw(c); 
       return bitmap; 
}