繁体   English   中英

获取ImageView的图像并将其发送到使用Intent的活动

[英]get ImageView's image and send it to an activity with Intent

我的应用程序中有许多产品网格。 当用户选择网格中的一个项目时,我将开始一个新的活动作为DIALOG框并显示项目的名称,数量和图像。 但我无法动态发送图像源。

这是我的代码

gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
            //Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show();
            Intent item_intent = new Intent(MainActivity.this, Item.class);
            item_intent.putExtra("name",((TextView) v.findViewById(R.id.grid_item_label)).getText());
            item_intent.putExtra("quantity",((TextView) v.findViewById(R.id.grid_item_quantity)).getText());

            //my problem is here***************************************************
            ImageView my_image =  findViewById(R.id.grid_item_image).getDrawable();
            item_intent.putExtra("image",my_image.getXXXXX());
            //*********************************************************************
            MainActivity.this.startActivity(item_intent);

        }
    });

我应该使用什么来从ImageView获取图像源?

使用Bundle之类的

imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();

 Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);


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

image.setImageBitmap(bmp );

您可以将Parcable(例如位图)或可序列化对象放在一个包中,并从另一个活动中的Bundle中检索该对象。

你如何使用bundle传递android活动之间的图像(位图)?

虽然我不推荐它,因为你在活动之间传递了大量数据。 我建议您将图像引用ID传递给下一个活动并在那里检索图像。

如何将selectedListItem的对象传递给另一个活动?

编辑:

intent.putExtra("imageId", imageId);

在其他活动中:

int imageRef = getIntent().getIntExtra("imageId", defaultValue);

http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29

您需要使用解决方案将drawable转换为Bitmap:然后您可以通过intent将其传递给下一个活动,因为Bitmap类实现了Parcelable接口。

首先,您必须先保存图像,然后才能使用此方法发送已保存图像的文件名

    void Send() { 
            if (file != null) {
            Intent intent = new Intent(this, Draw.class);
            intent.putExtra(PICTURE_FILE_ID, file);
            startActivity(intent);
        } else if (file == null) {
            Toast tst = Toast.makeText(getApplication(),
                    "Please Click Save First", Toast.LENGTH_SHORT);
            tst.setGravity(Gravity.CENTER, 0, 0);
            tst.show();
        }
}

另一项活动

使用mBitmapFile = (File) getIntent().getSerializableExtra( YourClassName.PICTURE_FILE_ID);

您可以将drawable转换为位图,然后转换为字节数组,并按意图传递此字节数组。

Drawable d;  
Bitmap b= ((BitmapDrawable)d).getBitmap();

int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);  
b.copyPixelsToBuffer(buffer);  
byte[] array = buffer.array();

用户这段代码:

呼叫活动......

Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);

......并在你的接收活动中

if(getIntent().hasExtra("byteArray")) {
    ImageView previewThumbnail = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length); 
}

在活动之间传递位图数据时,我通常更喜欢将其存储在扩展的Application类中,您可以从所有活动中访问该类。 显然可以像其他人所说的那样通过将其传递给Intent来实现,但如果您需要在多个活动中使用它,将它存储在应用程序中会为您提供更大的灵活性。

我有同样的问题,并通过意图传递所选项目的位置来解决它。 正如我认为并从堆栈专业人士那里阅读它是最好的解决方案。 只需在包含GridView的FirstActivity.java中执行此操作

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = new Intent(getActivity(),DetailActivity.class);
    intent.putExtra("id",position);
     startActivity(intent);    } });

然后在目标Activity中,你必须从extras获取位置并从数据库中提取它或者你的图像数组被转换为:

Intent intent = getActivity().getIntent();
            int position = intent.getIntExtra("id",0);
            ImageAdapter imageAdapter = new ImageAdapter(getActivity());
            ImageView imageView = (ImageView)rootView.findViewById(R.id.movie_detail_image);
            imageView.setImageResource((int)imageAdapter.getItem(position));

您必须编辑图像适配器getItem方法以返回所选的图像ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM