2

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?

pengwang
  • 19,536
  • 34
  • 119
  • 168

2 Answers2

3

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.

idiottiger
  • 5,147
  • 2
  • 25
  • 21
  • sorry,no effect.i also using Bitmap.Config.RGB_565 – pengwang Jan 11 '12 at 00:45
  • Excellent! but I have tried several times using this code with failure, cause I was missing one thing...to add this permission into Manifest file: Thanks. – Mehdi Aug 14 '13 at 06:54
0

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...

Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59
  • i have tried and works for me. if i want to take screen shot of particular view and save it as bitmap is possible with this. – Hiren Dabhi Jan 10 '12 at 09:31
  • yes,if used common environment it correct,but used catch Camera preview it fail,the save file jpeg is black,but imageview show correct – pengwang Jan 10 '12 at 10:12