1

I have the Relative Layout which has a scroll view and has more than 20 textviews. when i try to convert this layout into a bitmap it is not happening when i view it in an external storage it says "this file is damaged" in gallery the image shows a black view

this is the code which i have used to convert the view to bitmap

public static Bitmap loadBitmapFromView(Context context, View v) {

DisplayMetrics dm = context.getResources().getDisplayMetrics();
v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
        v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);

return bitmap;
     }

I have passed the object of relative layout to loadBitmapFromView.

and I have used this code for saving image to external storage

     private void SaveImage(Bitmap finalBitmap) {


String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 100000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
    FileOutputStream out = new FileOutputStream(file);
    finalBitmap.compress(Bitmap.CompressFormat.JPEG,1000,out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
   }

this is my logcat

12-30 15:02:20.397 24825-24825/com.google.android.gms.samples.vision.barcodereader W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
12-30 15:02:20.397 24825-24825/com.google.android.gms.samples.vision.barcodereader W/System.err:     at com.google.android.gms.samples.vision.barcodereader.ResultActivity.SaveImage(ResultActivity.java:3483)
12-30 15:02:20.397 24825-24825/com.google.android.gms.samples.vision.barcodereader W/System.err:     at com.google.android.gms.samples.vision.barcodereader.ResultActivity.onCreate(ResultActivity.java:137)
Ravi
  • 34,851
  • 21
  • 122
  • 183
Akash Dubey
  • 1,508
  • 17
  • 34
  • How you are calling SaveImage function and from where ? – Triode Dec 30 '15 at 10:05
  • i think your problem has been addressed already in other thread go here and have a look on this thread [link](http://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android) – Zain Ul Abidin Dec 30 '15 at 10:11
  • @Triode I am calling it from onCreate method of same class using this line - loadBitmapFromView(getApplicationContext(), v); – Akash Dubey Dec 30 '15 at 10:14

2 Answers2

2

Your createBitmap function would be returning null, You try to obtain the bitmap like below.

final View v = mView.findViewById(R.id.your_view_id);
Bitmap b = Bitmap.createBitmap( v.getMeasuredWidth(),
                v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
Triode
  • 11,309
  • 2
  • 38
  • 48
1

Try this

public static Bitmap loadBitmapFromView(View v) {   
Bitmap b = Bitmap.createBitmap( 
    v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);           
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);   
  return b;
 }

and use it as

Bitmap b = loadBitmapFromView(your_layout_object);

Kishan Soni
  • 816
  • 1
  • 6
  • 19