0

I am generating an image on a button click. The layout used to create the image from is not currently visible inside the activity. A vector image is a background of the layout but all I see is black image.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="370dp"
    android:layout_height="400dp"
    android:background="@drawable/ic_background">

    <TextView
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:textColor="@color/white"
        android:textSize="40sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_height="wrap_content"/>
</LinearLayout> 

Generating image

private void generateAndShareImage(){
    final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View view = inflater.inflate(R.layout.image_address, null);
    final TextView addressView = (TextView) view.findViewById(R.id.address);
    addressView.setText("Test Address");

    final Bitmap cardImage = getBitmapFromView(view);
    //store image in external storage
    String savedImagePath = storeImage(cardImage);
    if(savedFilePath != null){
        scanGallery(savedImagePath);
    }
}

private void scanGallery(final String path) {
    try {
        MediaScannerConnection.scanFile(getBaseContext(), new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            }
        });
    } catch (Exception e) {
        Log.e("Error scanning gallery",e.getMessage());
    }
}

private Bitmap getBitmapFromView(@NonNull  final View view){
    final Bitmap bitmap = Bitmap.createBitmap(370, 400, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

What is wrong?

mortalis
  • 2,060
  • 24
  • 34
Coder
  • 3,090
  • 8
  • 49
  • 85

1 Answers1

1

You need to measure and layout your views first:

int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
view.measure(spec, spec); 
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap cardImage = getBitmapFromView(view);

Above code should work.

Its better to pass the dimensions of measured view in getBitmapFromView(view, width, height) to be used for Bitmap.createBitmap() instead of using hard coded values.

Check this out for other tweaks Converting a view to Bitmap without displaying it in Android?

BTW you can't use 370 as its is, if you really need to use a hard coded figure then use this instead:

float dp_370 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 370, context.getResources().getDisplayMetrics());

To understand how density independent pixels work in Android, please read: What is the difference between "px", "dp", "dip" and "sp" on Android?

and

https://material.io/guidelines/layout/units-measurements.html#units-measurements-scaleable-pixels-sp

EDIT:

Following is a working code sample that I am using to see the generated bitmap in ImageView:

final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View test = inflater.inflate(R.layout.test, null);
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
test.measure(spec, spec);
int measuredWidth = test.getMeasuredWidth();
int measuredHeight = test.getMeasuredHeight();
test.layout(0, 0, measuredWidth, measuredHeight);
final Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
test.draw(canvas);

//now show this inside an ImageView
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bitmap);
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • Thanks but it still does not work. I do not see the drawable in background and not text either. Just the color of image is now changed – Coder May 26 '17 at 08:09
  • @Coder I updated my answer with working sample that I am using to see the generated bitmap. May be the issue is with your `storeImage()` code. – M-Wajeeh May 26 '17 at 08:51