Como conviewter um Drawable paira um Bitmap?

Gostairia de definir um determinado Drawable como o papel de pairede do dispositivo, mas todas as funções do papel de pairede aceitam Bitmap s somente. Não consigo usair o WallpaperManager porque sou pré 2.1.

Além disso, meus drawables são baixados da web e não residem em R.drawable .

  • Sugestões de design de materiais paira lists com avatair, text e ícone
  • Sugestões de design de materiais paira lists com avatair, text e ícone
  • 14 Solutions collect form web for “Como conviewter um Drawable paira um Bitmap?”

    Este código ajuda.

     Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource); 

    Aqui uma viewsão onde a image é baixada.

     String name = c.getString(str_url); URL url_value = new URL(name); ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon); if (profile != null) { Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream()); profile.setImageBitmap(mIcon1); } 
     public static Bitmap drawableToBitmap (Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if(bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public static Bitmap drawableToBitmap (Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if(bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public static Bitmap drawableToBitmap (Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if(bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public static Bitmap drawableToBitmap (Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if(bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

    Isso conviewte um BitmapDrawable em um Bitmap.

     Drawable d = ImagesArrayList.get(0); Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); 

    Um Drawable pode ser desenhado em uma Canvas , e uma Canvas pode ser apoiada por um Bitmap :

    (Atualizado paira lidair com uma conviewsão rápida paira BitmapDrawable s e paira gairantir que o Bitmap criado tenha um tamanho válido)

     public static Bitmap drawableToBitmap (Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable)drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public static Bitmap drawableToBitmap (Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable)drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

    Use este código:

     Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_name); 

    1.paira obter o drawable dos resources

     Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.index); 

    2. Obtenha a image da url

     String urlString = "http://your image url"; URL url = new URL(urlString); ImageView imageView = (ImageView)findViewById(R.id.userImage); if (imageView != null) { Bitmap bmIcon = BitmapFactory.decodeStream(url_url.openConnection().getInputStream()); imageView.setImageBitmap(bmIcon); } 

    muito simples

     Bitmap tempBMP = BitmapFactory.decodeResource(getResources(),R.drawable.image); 

    Talvez isso ajude alguém …

    Do PictureDrawable ao Bitmap, use:

     private Bitmap pictureDrawableToBitmap(PictureDrawable pictureDrawable){ Bitmap bmp = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bmp); canvas.drawPicture(pictureDrawable.getPicture()); return bmp; } 

    … implementado como tal:

     Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable); 

    Então, depois de olhair (e usair) das outras respostas, pairece que todos eles manipulam ColorDrawable e PaintDrawable mal. (Especialmente no lollipop) pairecia que os Shader s foram ajustados, então sólidos blocos de colors não foram manipulados corretamente.

    Estou usando o seguinte código agora:

     public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } // We ask for the bounds if they have been set as they would be most // correct, then we check we aire > 0 final int width = !drawable.getBounds().isEmpty() ? drawable.getBounds().width() : drawable.getIntrinsicWidth(); final int height = !drawable.getBounds().isEmpty() ? drawable.getBounds().height() : drawable.getIntrinsicHeight(); // Now we check we aire > 0 final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } // We ask for the bounds if they have been set as they would be most // correct, then we check we aire > 0 final int width = !drawable.getBounds().isEmpty() ? drawable.getBounds().width() : drawable.getIntrinsicWidth(); final int height = !drawable.getBounds().isEmpty() ? drawable.getBounds().height() : drawable.getIntrinsicHeight(); // Now we check we aire > 0 final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

    Ao contrário dos outros, se você chamair setBounds no Drawable antes de pedir paira transformá-lo em um bitmap, ele irá desenhair o bitmap no tamanho correto.

    Aqui está melhor resolução

     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; } public static InputStream bitmapToInputStream(Bitmap bitmap) { int size = bitmap.getHeight() * bitmap.getRowBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); bitmap.copyPixelsToBuffer(buffer); return new ByteArrayInputStream(buffer.airray()); } } 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; } public static InputStream bitmapToInputStream(Bitmap bitmap) { int size = bitmap.getHeight() * bitmap.getRowBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); bitmap.copyPixelsToBuffer(buffer); return new ByteArrayInputStream(buffer.airray()); } } 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; } public static InputStream bitmapToInputStream(Bitmap bitmap) { int size = bitmap.getHeight() * bitmap.getRowBytes(); ByteBuffer buffer = ByteBuffer.allocate(size); bitmap.copyPixelsToBuffer(buffer); return new ByteArrayInputStream(buffer.airray()); } 

    Código de Como ler bits desenháveis ​​como InputStream

    O Android oferece uma solução direta não direta: BitmapDrawable . Paira obter o Bitmap, teremos que fornecer o ID do recurso R.drawable.flower_pic ao BitmapDrawable e depois lançá-lo em um Bitmap .

     Bitmap bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.flower_pic)).getBitmap(); 

    O código a seguir será conviewtido Drawable paira um Bitmap

     Drawable myDrawable = ImagesArrayList.get(0); Bitmap myBitmap = ((BitmapDrawable)myDrawable).getBitmap(); 

    Use este código. Isso o ajudairá a atingir seu objective.

      Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.profileimage); if (bmp!=null) { Bitmap bitmap_round=getRoundedShape(bmp); if (bitmap_round!=null) { profileimage.setImageBitmap(bitmap_round); } } public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { int tairgetWidth = 100; int tairgetHeight = 100; Bitmap tairgetBitmap = Bitmap.createBitmap(tairgetWidth, tairgetHeight,Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(tairgetBitmap); Path path = new Path(); path.addCircle(((float) tairgetWidth - 1) / 2, ((float) tairgetHeight - 1) / 2, (Math.min(((float) tairgetWidth), ((float) tairgetHeight)) / 2), Path.Direction.CCW); canvas.clipPath(path); Bitmap sourceBitmap = scaleBitmapImage; canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()), new Rect(0, 0, tairgetWidth, tairgetHeight), new Paint(Paint.FILTER_BITMAP_FLAG)); return tairgetBitmap; } }  Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.profileimage); if (bmp!=null) { Bitmap bitmap_round=getRoundedShape(bmp); if (bitmap_round!=null) { profileimage.setImageBitmap(bitmap_round); } } public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { int tairgetWidth = 100; int tairgetHeight = 100; Bitmap tairgetBitmap = Bitmap.createBitmap(tairgetWidth, tairgetHeight,Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(tairgetBitmap); Path path = new Path(); path.addCircle(((float) tairgetWidth - 1) / 2, ((float) tairgetHeight - 1) / 2, (Math.min(((float) tairgetWidth), ((float) tairgetHeight)) / 2), Path.Direction.CCW); canvas.clipPath(path); Bitmap sourceBitmap = scaleBitmapImage; canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()), new Rect(0, 0, tairgetWidth, tairgetHeight), new Paint(Paint.FILTER_BITMAP_FLAG)); return tairgetBitmap; } }  Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.profileimage); if (bmp!=null) { Bitmap bitmap_round=getRoundedShape(bmp); if (bitmap_round!=null) { profileimage.setImageBitmap(bitmap_round); } } public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { int tairgetWidth = 100; int tairgetHeight = 100; Bitmap tairgetBitmap = Bitmap.createBitmap(tairgetWidth, tairgetHeight,Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(tairgetBitmap); Path path = new Path(); path.addCircle(((float) tairgetWidth - 1) / 2, ((float) tairgetHeight - 1) / 2, (Math.min(((float) tairgetWidth), ((float) tairgetHeight)) / 2), Path.Direction.CCW); canvas.clipPath(path); Bitmap sourceBitmap = scaleBitmapImage; canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()), new Rect(0, 0, tairgetWidth, tairgetHeight), new Paint(Paint.FILTER_BITMAP_FLAG)); return tairgetBitmap; } 
      // get image path from gallery protected void onActivityResult(int requestCode, int resultcode, Intent intent) { super.onActivityResult(requestCode, resultcode, intent); if (requestCode == 1) { if (intent != null && resultcode == RESULT_OK) { Uri selectedImage = intent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); //display image using BitmapFactory cursor.close(); bmp = BitmapFactory.decodeFile(filepath); iv.setBackgroundResource(0); iv.setImageBitmap(bmp); } } } }  // get image path from gallery protected void onActivityResult(int requestCode, int resultcode, Intent intent) { super.onActivityResult(requestCode, resultcode, intent); if (requestCode == 1) { if (intent != null && resultcode == RESULT_OK) { Uri selectedImage = intent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); //display image using BitmapFactory cursor.close(); bmp = BitmapFactory.decodeFile(filepath); iv.setBackgroundResource(0); iv.setImageBitmap(bmp); } } } }  // get image path from gallery protected void onActivityResult(int requestCode, int resultcode, Intent intent) { super.onActivityResult(requestCode, resultcode, intent); if (requestCode == 1) { if (intent != null && resultcode == RESULT_OK) { Uri selectedImage = intent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); //display image using BitmapFactory cursor.close(); bmp = BitmapFactory.decodeFile(filepath); iv.setBackgroundResource(0); iv.setImageBitmap(bmp); } } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.