pengwang
pengwang

Reputation: 19946

save bitmap as a jpeg file

this question is easy,we can :

public static void writePhotoJpg(Bitmap data, String pathName) {
    File file = new File(pathName);
    try {
        file.createNewFile();
        // BufferedOutputStream os = new BufferedOutputStream(
        // new FileOutputStream(file));

        FileOutputStream os = new FileOutputStream(file);
        data.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();


    } catch (Exception e) {
        e.printStackTrace();
    }
}

but this method not all successful:because "Note: not all Formats support all bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth, and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels)." in the google document. without luck i have this problem: in the Camera preview i used :

 private Bitmap  printScreen() {
    View view = this.getWindow().getDecorView();
    // if (view.isDrawingCacheEnabled()) {
    view.setDrawingCacheEnabled(true);
    Calendar c = Calendar.getInstance();
    String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
    // }
    view.buildDrawingCache();

    Bitmap bmp = view.getDrawingCache();

    return  bmp ;
}

so when i used setImageBitmap(bmp) ,looks very well,but when i open the save jpeg file it is a black jpeg.so i think the save method is not well,can you tell me another save method?

Upvotes: 2

Views: 12052

Answers (2)

idiottiger
idiottiger

Reputation: 5177

you can try this:

 public static final int BUFFER_SIZE = 1024 * 8;
 static void writeExternalToCache(Bitmap bitmap, File file) {
    try {
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE);
        bitmap.compress(CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {

    }

}

it works in my code, so if it's not work, try to find error:

1.the bitmap display right ?

2.where is the bitmap save file? the file has some limit? like size or ...

if the error is your post reason, you can try decode the bitmap again using Bitmap.Config.RGB_565.

Upvotes: 3

Hiren Dabhi
Hiren Dabhi

Reputation: 3713

Use .getRootView() method of view OR the layout contains this view, use it (e.g.) mainLayout contain one image view at index 1 then mainlayou.getChildAt(1) to get the view.

E.g.

View v1 = mainLayout.getChildAt(1);     //OR  View v1 = mainLayout.getRootView();   
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        return bitmap;

Hope helpful to you...

Upvotes: 0

Related Questions