Pergunta

I've created a Bitmap [Thumbnail] which is fetched after picking a video from gallery.

Snippet:-

bm= ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);

I'm putting these bitmap in Gallery adapter which is meant from images only, tht's y i'm creating thumbnail of Video and putting there. But

I want to show some difference between image and video in Gallery Strip which can be done by overlaying VideoThumbnail with something like Play Option.

Something like these

Tried to OverLay my Bitmap with Small Play Icon but it throws NullPointerException on Bitmap.CreateScaledBitmap(..)

Snippet:-

bm= ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);
Bitmap change = null;
Bitmap border = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_media_play);
int width = bm.getWidth();
int height = bm.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width/2,height/2, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);

Adding that newly Overlay Created bitmap on my adapter.

AddIPDActivity.this.data.add(bm);
Foi útil?

Solução

Created Overlay BitMap with LayerDrawable

Bitmap on which overlay need to be done.

bm = ThumbnailUtils.createVideoThumbnail(currentFileUri.getPath(),MediaStore.Video.Thumbnails.MICRO_KIND);

LayerDrawable applied on Bitmap with custom image on it.

Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = new BitmapDrawable(bm);
layers[1] = r.getDrawable(android.R.drawable.ic_media_play);
LayerDrawable layerDrawable = new LayerDrawable(layers);

Added Bitmap on Gallery - Adapter.

this.data.add(drawableToBitmap(geSingleDrawable(layerDrawable))); //data is adapter for Gallery.

Converting LayerDrawable into BitMap:-

LayerDrawable -> Drawable -> BitMap

  public static Drawable geSingleDrawable(LayerDrawable layerDrawable){
        int resourceBitmapHeight = 136, resourceBitmapWidth = 153;
        float widthInInches = 0.9f;
        int widthInPixels = (int)(widthInInches * SmartConsultant.getApplication().getResources().getDisplayMetrics().densityDpi);
        int heightInPixels = widthInPixels * resourceBitmapHeight / resourceBitmapWidth;
        int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
        layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);     
        Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
        layerDrawable.draw(canvas);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(SmartConsultant.getApplication().getResources(), bitmap);
        bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
        return bitmapDrawable;
}
    public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap); 
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

Outras dicas

u r getting NPE because

Bitmap change = null;
change = Bitmap.createScaledBitmap(change, width, height, false);

change is always null. U r passing a null value.So, before passing change as a parameter of createScaledBitmap(parameters) assign a bitmap to change after that call this

change = Bitmap.createScaledBitmap(change, width, height, false);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top