如何将ImageView从第一个活动共享并保存到第三个活动

时间:2017-11-11 05:06:39

标签: android android-intent imageview

活动1:我有一个图像视图,其中设置了从相机和图库中拍摄的图像,并且工作正常。在此活动中,有一个右键单击按钮,可以将您重定向到第二个活动。

活动2:在本活动中,我有四个选项

  1. 保存
  2. 共享
  3. 通过Instagram分享
  4. 通过Facebook分享
  5. 活动3:从以上四个选项中,第三个活动相应地起作用。 现在我不知道如何将第一个活动中拍摄的图像传递给第三个活动。

    我的努力:在从相机拍摄的第一张活动图像中:

     Intent i=new Intent(Camera.this,SaveVia.class);
     i.putExtra("image", thumbnail );
     startActivity(i);
    

    在第二个活动中SaveVia:

    Intent intent2 = new Intent(SaveVia.this, Save.class);
    Bitmap receiptimage = (Bitmap)getIntent().getExtras().getParcelable("image")
    startActivity(intent2);
    

    在第三个名为Save的活动中:

    Bitmap receiptimage = (Bitmap) getIntent().getExtras().getParcelable("imagepass");
           // receipt.setImageBitmap(receiptimage);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            receiptimage.compress(Bitmap.CompressFormat.PNG, 90, bytes);
            File storagePath = new File(Environment.getExternalStorageDirectory() + "/PhotoAR/");
            storagePath.mkdirs();
    
            File destination = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
    
            FileOutputStream fo;
            try {
                destination.createNewFile();
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
                fo.close();
                Toast.makeText(Save.this,"No Error",Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(Save.this,"Error Arrived",Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(Save.this,"Error Arrived again",Toast.LENGTH_LONG).show();
    

2 个答案:

答案 0 :(得分:2)

@AND 您不需要将位图作为Parcelable从一个activity.Pass文件路径从一个活动传递到另一个活动。

onActivityresult 中捕获图像获取路径并保存该路径。 并重定向那两个第二个活动。并从该路径显示您的位图。在Facebook和Instagram上分享从路径分享Uri。

答案 1 :(得分:1)

我建议使用File I / O将位图保存到磁盘。使用putExtra将文件的路径放入intent bundle中,然后在其他屏幕中重新创建位图。将大位图放入具有intent的bundle中可能会导致TransactionTooLarge异常。

查看Getting path of captured image in android using camera intent

相关问题