如何将imageview从一个活动发送到另一个活动

Ani*_*Ani 5 android android-intent imageview

我在第一个活动的listview中有一个imageview,我想将我的imageview发送到listview项目的clicl上的第二个活动.

我试过以下代码 -

将可绘制图像转换为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();
Run Code Online (Sandbox Code Playgroud)

通过意图发送 -

Intent intent=new Intent(PicturesList.this,PictureDetail.class);
                intent.putExtra("Bitmap", byteArray);
                startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

在第二次活动中 -

Bundle extras = getIntent().getExtras();
        byteArray = extras.getByteArray("Bitmap");
Run Code Online (Sandbox Code Playgroud)

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                        imageview.setImageBitmap(bmp);
Run Code Online (Sandbox Code Playgroud)

但问题在于 -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Run Code Online (Sandbox Code Playgroud)

这需要可绘制的图像,我有imageview,我可以将我的imageview转换为drawable吗?还是别的什么?如何发送imageview而不是drawable.之前有人这样做过.

这就是我在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();
Run Code Online (Sandbox Code Playgroud)

Mag*_*ode 4

您不需要将位图转换为字节数组。位图是可打包的,因此您只需将putParcelable(String, Parcelable)其添加到包中即可。

编辑:

例如:

Bundle extras = new Bundle();
extras.putParcelable("Bitmap", bmp);
intent.putExtras(extras);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

然后在第二个活动中:

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");
Run Code Online (Sandbox Code Playgroud)