如何从一个活动的其他派出的ImageView [英] how to send imageview from one activity to other

查看:92
本文介绍了如何从一个活动的其他派出的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表视图的ImageView的在第一个活动, 我想送我的ImageView到次活动的ListView项的clicl。

我曾尝试以下code -

转换绘制的图像为字节组: -

  BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
                ByteArrayOutputStream流=新ByteArrayOutputStream();
                bmp.com preSS(Bitmap.Com pressFormat.PNG,100,流);
                字节[]的字节数组= stream.toByteArray();
 

通过Intent-发送

 意向意图=新的意图(PicturesList.this,PictureDetail.class);
                intent.putExtra(位图,字节);
                startActivity(意向);
 

在第二个活动 -

 捆绑额外= getIntent()getExtras()。
        的字节数组= extras.getByteArray(位图);
 

  BMP位= BitmapFactory.de codeByteArray(字节数组,0,byteArray.length);
                        imageview.setImageBitmap(BMP);
 

但问题就在这里 -

  BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
 

这需要绘制的图像和我有ImageView的, 我可以将我的ImageView成绘制?或任何ALSE? 如何发送绘制的ImageView的替代。 任何人面前都做到了这一点。

这是我如何设置图像的ImageView

 新的AsyncTask<虚空,虚空,虚空>(){
            @覆盖
            保护无效doInBackground(虚空...... PARAMS){


                尝试 {
                    URL的newURL =新的URL(http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png);
                    位= BitmapFactory.de codeStream(newurl.openConnection()的getInputStream());
                    //位= Bitmap.createScaledBitmap(位图,50,50,真正的);
                }
                赶上(MalformedURLException的E){
                    e.printStackTrace();
                }赶上(IOException异常E){
                    e.printStackTrace();
                }
            //位= imageLoader.DisplayImage(http://farm3.static.flickr.com/2199/2218403922_062bc3bcf2.jpg,ImageView的);
                //位= Bitmap.createScaledBitmap(位图,imageview.getWidth(),imageview.getHeight(),TRUE);
                返回null;
            }
            @覆盖
            保护无效onPostExecute(无效的结果){
                super.onPostExecute(结果);
                imageview.setImageBitmap(位);
            }
        }。执行();
 

解决方案

您不必将位图转换为字节数组。位图是parcelable,所以你可以只使用<一href="http://developer.android.com/reference/android/os/Bundle.html#putParcelable%28java.lang.String,%20android.os.Parcelable%29"相对=nofollow> putParcelable(字符串,Parcelable) 将其添加到捆绑。

编辑:

例如:

 捆绑额外=新包();
extras.putParcelable(位图,BMP);
intent.putExtras(临时演员);
startActivity(意向);
 

然后在第二个活动:

 捆绑额外= getIntent()getExtras()。
BMP位=(位图)extras.getParcelable(位图);
 

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();

解决方案

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");

这篇关于如何从一个活动的其他派出的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆