【问题标题】:Parse jpeg image through intent extra android通过意图额外的android解析jpeg图像
【发布时间】:2017-05-01 13:03:06
【问题描述】:

我想通过intent extras解析一张图片,Intent成功打开第二个activity,解析文本字段数据但不解析图片:

如果有帮助,图片为 .jpg 格式。

我之前在 png 图像上使用过以下内容,并且似乎工作正常。

这是第一个活动:

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

                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                imageView.setDrawingCacheEnabled(true);
                bitmap = imageView.getDrawingCache(Boolean.parseBoolean(contactList.get(position).getProfilePic()));

                Intent intent = new Intent(MainActivity.this, DetailedActivity.class);
                intent.putExtra("imageView", bitmap);
                intent.putExtra("name", contactList.get(position).getName());
                intent.putExtra("email", contactList.get(position).getEmail());
                intent.putExtra("phone", contactList.get(position).getPhone().getMobile());

                startActivity(intent);

            }
        });

第二个活动:

public class DetailedActivity extends AppCompatActivity {

    //declared variables
    TextView txtName, txtEmail, txtPhone;
    Bundle img;
    String name;
    String email;
    String phone;
    ImageView imageView;
    Context mContext;
    Bitmap bitmap;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed);

        //Get Intent
        Intent i = getIntent();


        //items to receive intent
        name = i.getStringExtra("name");
        email = i.getStringExtra("email");
        phone = i.getStringExtra("phone");
        bitmap = i.getParcelableExtra("imageView");



        //Assign values to layout file items
        txtName = (TextView) findViewById(R.id.tvName);
        txtEmail = (TextView) findViewById(R.id.tvEmail);
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        imageView = (ImageView) findViewById(R.id.profilPic);



        /**
         * Set parsed Text
         */

        txtName.setText(name);
        txtEmail.setText(email);
        txtPhone.setText(phone);
        imageView.setImageBitmap(bitmap);

【问题讨论】:

  • 详细说明,“通过 Intent Extras 解析图像”是什么意思。请详细说明“但不是图像”是什么意思。请注意,您在这里使用的不是 PNG 或 JPEG,而是 Bitmap 对象。另请注意,通过 Intent extras 传递 Bitmap 对象是相当冒险的,因为您对 Intent 的大小有限制。与其尝试从ImageView 获取Bitmap(这可能有效也可能无效),而是传入有关图像来源的Intent 信息。
  • @CommonsWare,好的,所以我可以传递有关图像来源的信息,即 url,但我该怎么做呢?
  • 您的ImageView 要么是空的,要么不是。如果不是,那是因为您编写了一些 Java 代码将某些内容放入ImageView。该 Java 代码从某处获取图像:资源 ID、HTTP URL 等。将该数据(资源 ID、HTTP URL 等)传递给其他活动,以便它可以使用类似的 Java 代码。使用提供缓存的an image-loading library 的奖励积分。

标签: java android image-processing android-intent


【解决方案1】:

试试这个

第一个活动

bitmap = imageView.getDrawingCache(Boolean.parseBoolean(contactList.get(position).getProfilePic()));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

第二次活动

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

【讨论】:

  • 我试过这个,但我得到以下信息:尝试调用虚拟方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)'在空对象引用上
猜你喜欢
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多