Question

I need to create a custom view and then save it to png file into sdcard. Right now I am getting black colour images in sdcard. I couldn't trace out the issue in code. Can anyone please help me out.

layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical" >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />
</LinearLyout>

In my java file, I have inflated linear layout and set data to textviews and then I am calling:

private Bitmap convertViewToBitmap(LinearLayout layout) {
    layout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    int width = layout.getMeasuredWidth();
    int height = layout.getMeasuredHeight();

    //Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(width, 
            height, 
            Bitmap.Config.ARGB_8888);
    //Create a canvas with the specified bitmap to draw into
    Canvas c = new Canvas(bitmap);

    //Render this view (and all of its children) to the given Canvas
    view.draw(c);
    return bitmap;
}

After getting bitmap I am saving it to sdcard as follows:

private void saveBitmapTpSdCard(Bitmap bitmap, String fileName) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);

        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + getString(R.string.app_name));

        try {
            if(!f.exists()) {
                f.mkdirs();
            }
            File imageFile = new File(f, fileName + ".png");
            FileOutputStream fo = new FileOutputStream(imageFile);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}
Was it helpful?

Solution

I got it worked by adding this line:

view.layout(0, 0, width, height);

before creating Bitmap using Bitmap.createBitmap

OTHER TIPS

try the following code to convert a custom view to Bitmap Object:

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main_for_bitmap);
    layout.setDrawingCacheEnabled(true);
    layout.buildDrawingCache();
    Bitmap bitmap = layout.getDrawingCache();

The fact that LinearLayout was measured by you manually doesn't means that it was layouted (i.e. layout wasn't invoked). Therefore children are not getting layouted, therefore they don't receive correct position on a ViewGroup, therefore they're not visible.

Instead of trying to mimic drawing lifecycle, I would suggest you to call linearLayout.post() on your LinearLayout (if it's added to view hierarchy of course) and inside Runnable invoke your "snapping" workflow.

Alternatively you can invoke layout manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top