将图像从一个片段传递到另一个片段并在该片段中显示图像

时间:2016-01-18 05:07:28

标签: android image android-fragments

从中选择图像的第一个片段

           iv.setImageURI(Uri.fromFile(pictureFile));
                String stringUri;
                stringUri = pictureFile.toString();

                FreeFragment ldf = new FreeFragment ();
                Bundle args = new Bundle();
                args.putString("Image", stringUri);
                ldf.setArguments(args);
                Log.d("Passing image", String.valueOf(args));
                getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

第二个片段接收图像并显示

 String bbb = getArguments().getString("Image");
    Bitmap bitmap = BitmapFactory.decodeFile(bbb);
    iv.setImageBitmap(bitmap);

3 个答案:

答案 0 :(得分:6)

将文件路径发送到下一个片段

String stringUri = pictureFile.getAbsolutePath();

FreeFragment ldf = new FreeFragment ();
Bundle args = new Bundle();
args.putString("Image", stringUri);
ldf.setArguments(args);

getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

在你的NextFragment中你可以收到它并设置如下

String imgPath = getArguments().getString("Image");
Bitmap bitmap = BitmapFactory.decodeFile(new File(imgPath));
iv.setImageBitmap(bitmap);

答案 1 :(得分:1)

尝试将您的捆绑包放在第二个片段中

    Bundle bundle = this.getArguments();
if (bundle != null) {
    String s = bundle.getString(key, defaulValue);
}

另请查看此链接以获取进一步的参考 Passing bundle in fragments

答案 2 :(得分:0)

Bundle bundle = this.getIntent().getExtras();
String strPic = bundle.getString("Image");
File picFile = new File("your file path"+strPic);
if(picFile.exists())
{           
   iv.setImageURI(Uri.fromFile(picFile));
}