简体   繁体   中英

Dont work bitmap.compress

I want save image from web in inner folder (data/data/package_name/...), but I catch exception when I call Bitmap.compress() method. Please help me find my error. This is my code:

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) {}
    }

And this permissons in Manifest:

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

And this error printStackTrace() in logs:

    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

Try This Code

Bitmap relativeBitmap;

public void 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 Of save this method will be called

  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;
 }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM