【问题标题】:How to send image from one activity to another in android如何在android中将图像从一个活动发送到另一个活动
【发布时间】:2016-11-04 02:51:23
【问题描述】:

主要活动

holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       String title = ((TextView) view.findViewById(R.id.recipe_title)).getText().toString();
       String time = ((TextView) view.findViewById(R.id.time)).getText().toString();
       String servings = ((TextView) view.findViewById(R.id.servings)).getText().toString();
       String calories = ((TextView) view.findViewById(R.id.calories)).getText().toString();
       ImageView thumbnail = (ImageView) view.findViewById(R.id.recipe_img);
       Intent intent = new Intent(context, ActivityDetail.class);
       intent.putExtra("RecipeTitle", title);
       intent.putExtra("RecipeTotalTime", time);
       intent.putExtra("RecipeServings", servings);
       intent.putExtra("RecipeCalories", calories);
       context.startActivity(intent);
    }
});

详细活动

TextView time = (TextView)findViewById(R.id.time_detail);
TextView servings = (TextView)findViewById(R.id.servings_detail);
TextView calories = (TextView)findViewById(R.id.calories_detail);
ImageView thumbnail = (ImageView)findViewById(R.id.image_paralax);
time.setText(getIntent().getExtras().getString("RecipeTotalTime"));
servings.setText(getIntent().getExtras().getString("RecipeServings"));
calories.setText(getIntent().getExtras().getString("RecipeCalories"));

如何将缩略图从第一个 Activity 发送到第二个 Activity。在第二个 `Activity 中,缩略图将显示在 ImageView 上。

【问题讨论】:

标签: java android image android-studio send


【解决方案1】:

您不应尝试通过 Intent 发送图像。相反,最好发送图像的 Uri。在第二个活动中,从 Uri 加载图像。

【讨论】:

  • 这取决于您加载图像的方式(即路径、文件),如果您使用的是文件,您可以获得这样的 Uri。 Uri retrivedUri = Uri.fromFile(imageFile);然后将其作为 Parcelable 放入意图中。 myIntent.putParcelable(”yourKey”, retrivedUri);
【解决方案2】:

我不建议您将位图传递给其他活动,而是应该传递字符串 url,并且在接收活动中您可以从网络或文件加载图像。

如果您真的只想使用位图传递图像,那么您必须先将其转换为字节数组,然后再将其添加到意图、发送出去并解码。

Bitmap bmp = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);

然后在您的接收活动中,您需要添加以下代码来接收字节数组:

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

现在你有一个位图,你可以添加setImageDrawable(new BitmapDrawable(mContext.getResources(), bmp));

这就是你需要做的。

【讨论】:

    【解决方案3】:

    将图像发送到意图中并不是一件好事,您应该尝试发送图像的uri。

    但如果你真的想这样做,那么你可以从图像视图中获取可绘制的 Bitmap:

    Bitmap bitmap = ((BitmapDrawable) thumbnail.getDrawable()).getBitmap();
    

    由于位图是 Parcelable,您可以将其作为 Extra 直接发送:

    intent.putExtra("RecipeThumbnail", bitmap); 
    

    【讨论】:

      【解决方案4】:

      是的,有可能……但不推荐

      1)首先你必须从该位图制作位图和字节数组

      Bitmap bitmap = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
      ByteArrayOutputStream stream=new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
      byte[] image=stream.toByteArray();
      

      2) 然后你可以使用意图传递它

      intent.putExtra("Thumbnail Image", image);
      

      3) 在接收活动中,

      byte[] = getIntent.getExtra("Thumbnail Image");
      Bitmap bmp = BitmapFactory.decodeByteArray(byte, 0, byteArray.length);
      imageView.setImageBitmap(bmp);
      

      【讨论】:

        【解决方案5】:

        像大师一样做的更快:)

        传递图像的 id,你的资源中有它,那么如果活动 b 可以通过知道 id 来加载它,为什么 seralisin 这么多信息...

        Intent intent = new Intent(context, ActivityDetail.class);
        ...
        intent.putExtra("imageId", R.id.recipe_img);
        

        在其他活动上就足够了:

        ImageView thumbnail = (ImageView) view.findViewById(yourREceivedImageId);
        

        【讨论】:

          猜你喜欢
          • 2012-07-20
          • 2014-11-18
          • 1970-01-01
          • 2016-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-21
          相关资源
          最近更新 更多