添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
千年单身的猴子  ·  springboot jar ...·  1 年前    · 
乐观的皮带  ·  TEM数据分析系列课程 ...·  1 年前    · 
活泼的匕首  ·  最新招聘-材料学院·  1 年前    · 

I want to get Bitmap from ImageView . I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView .

Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
    bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
    Drawable d = mImageViewer.getDrawable();
    bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.draw(canvas);
storeImage(bitmap,"final.jpeg");
                Your code should work, make sure your image view has a drawable, if you're setting the image using setBackground use setBitmap instead
                    – elmorabea
                Nov 26 '14 at 11:42
imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
                Could you explain why you invalidate first? Is there a good reason to do this?
                    – Wolfgang Schreurs
                Sep 20 '18 at 12:54

Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)

Could be that the emulator or device your using has a different density and is trying to pull images from another folder.

If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html

this doesn't work perfectly all the time, usually give Canvas: trying to use recycled bitmap – Arsalan Saleem Feb 21 '15 at 8:08 This solution along with this answer(stackoverflow.com/questions/5566758/…) worked for me: imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); – Roy Jun 1 '15 at 12:58

If you are trying to get bitmap from Glide loaded image then this will help you

 Drawable dr = ((ImageView) imView).getDrawable();
        Bitmap bmp =  ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();

Take a picture of the ImagView and convert it to a string to send to the server

    ImageView   ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
                    getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
public String getStringImage(Bitmap bm){
    ByteArrayOutputStream ba=new ByteArrayOutputStream(  );
    bm.compress( Bitmap.CompressFormat.PNG,90,ba );
    byte[] by=ba.toByteArray();
    String encod= Base64.encodeToString( by,Base64.DEFAULT );
    return encod;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.4.12.33308