【问题标题】:Android - how can i transfer ImageView from one activity to another activity?Android - 如何将 ImageView 从一个活动转移到另一个活动?
【发布时间】:2013-08-04 09:34:05
【问题描述】:

我有 Activity1,它有一个 ImaveView,包括图像预览。 一旦我按下按钮,我就会从 Activity1 转到 Activity2。在 Activity2 中,我没有图像预览,但有一个选项按钮“保存 Activit1 的图像?”是或否。

目前我正在以错误的方式进行操作,例如将图像保存在磁盘中,然后从磁盘中读取。但是有什么办法不保存图像我可以将一个 imageView 从 Activity1 传输到 Activity2 吗?

这是我在 Activity1 > ImageView 中获取图片的方法,然后需要将其移至 Activity2。有什么想法吗?

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      this.imageView = (ImageView)this.findViewById(R.id.picture);            
      Button photoButton = (Button) this.findViewById(R.id.capture_btn);      
      Button btnShareToEmail = (Button) this.findViewById(R.id.btnshare);
      btnShareToEmail.setOnClickListener(this);

      //photoButton.setOnClickListener(new View.OnClickListener() {
          //@Override
        //  public void onClick(View v) {
          //    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            //  startActivityForResult(cameraIntent, CAMERA_REQUEST); 
         // }
      //});

      // without frozen
      new Handler().postDelayed(new Runnable() { public void run() { 
          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
          startActivityForResult(cameraIntent, CAMERA_REQUEST);
      }}, 100);      
  }



  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);



    }  
  }

编辑:

// 设置-Activity1

Intent winShare = new Intent(getBaseContext(), Activity2.class);
winShare.putExtra("Title", "r2.jpg");
winShare.putExtra("image1", photo);         
//int image_link = getIntent().getIntExtra("image1");
startActivityForResult(winShare,0); 

// GET - Activity2

    Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("image1"); // BITMAP_SHARED_KEY = "bitmap_shared_key"

    imageView.setImageBitmap(bitmap);
    // save it
    imageView.buildDrawingCache();
    Bitmap bm=imageView.getDrawingCache();
    OutputStream fOut = null;
    Uri outputFileUri;
    try {
      File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MYAPPPPPPPPPS" + File.separator);
      root.mkdirs();
      File sdImageMainDirectory = new File(root, "myPicName.jpg");
      outputFileUri = Uri.fromFile(sdImageMainDirectory);
      fOut = new FileOutputStream(sdImageMainDirectory);
      bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
      fOut.flush();
      fOut.close();      
   } catch (Exception e) {
     Toast.makeText(this, "Error occured. Please try again later.",Toast.LENGTH_SHORT).show();
   }

【问题讨论】:

  • 你必须在开始另一个活动时执行putExtra,并传输图像的路径(如果它来自画廊或相机)或(R.drawable.???)的int值

标签: java android android-intent


【解决方案1】:

有很多方法可以解决这个问题。这里有两种简单的方法:

第一个,你可以阅读更多关于SharedPreferences

第二个,你可以把Extra bitmap从这个Activity放到另一个Activity,像这样:

额外的:

intent.putExtra(BITMAP_SHARED_KEY, yourBitmap);
startActivity(intent);

getBitmap 你已经分享了

Bitmap bitmap = (Bitmap) intent.getParcelableExtra(BITMAP_SHARED_KEY); // BITMAP_SHARED_KEY = "bitmap_shared_key"

【讨论】:

    【解决方案2】:

    首先,您需要了解,当您以这种方式使用相机活动获取图像时,您将获得所拍摄图像的缩略图版本。为了获得必须保存的全尺寸图像,您可以查看我就此事撰写的这篇博文以获取更多信息:

    Use Camera Activity for Thumbnail and Full Size Image

    现在对于您的问题,我认为最好的选择是将此图像保存到文件中(在指南中描述)并使用图像的字符串路径将额外内容传递给以下活动。

    【讨论】:

      【解决方案3】:

      简单的方法使用这个

      1. 第一个活动

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.men_icon);
        Bundle extras = new Bundle();
        Intent intent=new Intent(FirstActivity.class,SecondActivity.class);
        extras.putParcelable("Bitmap", bitmap);
        intent.putExtras(extras);
        startActivity(intent);
        
      2. 第二个活动

         ImageView iv_photo=(ImageView)findViewById(R.id.iv_photo);
         Bundle extras = getIntent().getExtras();
         Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");
         iv_photo.setImageBitmap(bmp);
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-05
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        • 2018-02-04
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        相关资源
        最近更新 更多