Getting Image from ImageView

若如初见. 提交于 2019-11-28 07:25:05

To fetch currently selected view use

Gallery.getSelectedView(); 

and for getting Drawable from imageView use:

ImageVIew.getDrawable()

If you want to get inputstream from the drawable use following:

BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable .getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
    l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView textView=(TextView)view.findViewById(R.id.textView);
            ImageView imageView=(ImageView)view.findViewById(R.id.imageView);
            String textViewString=textView.getText().toString();
            Bitmap image=((BitmapDrawable)imageView.getDrawable()).getBitmap();

            DialogClass dialogClass=new DialogClass(MainActivity.this,image,textViewString);
            dialogClass.show();
        }
    });

My Best Function

public class MainActivity extends Activity {

    private ImageView imgView,bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgView=(ImageView) findViewById(R.id.imgView);
        bitmap=(ImageView) findViewById(R.id.bitmap);

        //set view to bitmap image
        bitmap.setImageBitmap(convertImageViewToBitmap(imgView));
    }

    //function to convert imageView to Bitmap

    private Bitmap convertImageViewToBitmap(ImageView v){

        Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap();

        return bm;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!