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