how to send imageview from one activity to other

久未见 提交于 2019-12-01 03:43:54

问题


I have an imageview in listview in first activity, I want to send my imageview into second activity on clicl of listview item.

I have tried following code-

Convert drawable image into bytearray:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

Sending through Intent-

Intent intent=new Intent(PicturesList.this,PictureDetail.class);
                intent.putExtra("Bitmap", byteArray);
                startActivity(intent);

In second activity-

Bundle extras = getIntent().getExtras();
        byteArray = extras.getByteArray("Bitmap");

and

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                        imageview.setImageBitmap(bmp);

But Problem is here-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

This require drawable image and i have imageview, Can I convert my imageview into drawable? or anything alse? How to send imageview instead of drawable. Anybody have done this before.

This is how I set image in imageview

new AsyncTask<Void,Void,Void>() {
            @Override
            protected Void doInBackground(Void... params) {


                try {
                    URL newurl = new URL("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
                    bitmap= BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
                    //bitmap = Bitmap.createScaledBitmap(bitmap, 50,50, true);
                }
                catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            //  bitmap=imageLoader.DisplayImage("http://farm3.static.flickr.com/2199/2218403922_062bc3bcf2.jpg", imageview);
                //bitmap = Bitmap.createScaledBitmap(bitmap, imageview.getWidth(), imageview.getHeight(), true);
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                imageview.setImageBitmap(bitmap);
            }
        }.execute();

回答1:


You don't need to convert the Bitmap to a byte array. Bitmap is parcelable so you can just use putParcelable(String, Parcelable) to add it to the Bundle.

Edit:

For example:

Bundle extras = new Bundle();
extras.putParcelable("Bitmap", bmp);
intent.putExtras(extras);
startActivity(intent);

Then in the second activity:

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");



回答2:


I thing you pass image ID and set Id in Next Activity Imageview E.g.

GEt ID  ((ImageView) v).getId();
SET ID  imageView.setImageResource(imgId);



回答3:


You can convert your ImageView To Bitmap.Try this

Bitmap bitmap = Bitmap.createBitmap(imageView .getMeasuredWidth(),imageView .getMeasuredHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
ImageView .draw(canvas);


来源:https://stackoverflow.com/questions/12858373/how-to-send-imageview-from-one-activity-to-other

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