【问题标题】:how to add image in one fragment and display it in another fragment/pass image through bundle in android如何在一个片段中添加图像并将其显示在另一个片段中/通过android中的捆绑传递图像
【发布时间】:2021-12-16 18:54:16
【问题描述】:

我想在一个片段中选择图像并将其显示在另一个片段中。
我怎样才能做到这一点。
我的代码是这样,但不工作

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, 100);
        }
    });


@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    Uri imageUri;
    if (resultCode == RESULT_OK && requestCode == 100){
        imageUri = data.getData();
        file1path = imageUri.getPath();
        TVfrontimg.setText(file1path);
    }
}

我使用 bundle 发送这个

Bundle bundle = new Bundle();
bundle.putString("image1", file1path);

registerPartThreeFragment.setArguments(bundle);
transaction.replace(R.id.FLcontainer, registerPartThreeFragment);
transaction.addToBackStack(null);
transaction.commit();

在另一个片段中

String imgPath = getArguments().getString("image1");
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(new File(imgPath)));
imageview.setImageBitmap(bitmap);

但它不起作用。请帮助。


它给了我这个错误

2020-05-30 00:16:20.808 8995-8995/com.example.deliveryapp E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/raw/storage/emulated/0/DCIM/Camera/IMG_20200319_173033.jpg (没有这样的文件或目录)

【问题讨论】:

    标签: android


    【解决方案1】:

    请使用getDataString() 而不是getData()

    if (resultCode == RESULT_OK && requestCode == 100){
            Uri imageUri = Uri.parse(data.getDataString());
            //imageUri = data.getData();
            file1path = imageUri.getPath();
            TVfrontimg.setText(file1path);
    }
    

    在将其设置为 ImageView 时,使用 ContentResolver 和输入流,如下所示:

    ContentResolver cR = getApplication().getContentResolver();
            try {
                InputStream ip = cR.openInputStream(imageUri);
                Bitmap bmp = BitmapFactory.decodeStream(ip);
                imageView.setImageBitmap(bmp)
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    

    您必须这样做,因为您获得的 contentUri 与文件 Uri 不同。

    希望这会有所帮助。

    【讨论】:

    • 我必须将图像发送到另一个片段。我如何将 imageuri 发送到另一个片段
    • 以与您现在相同的方式发送它..使用捆绑包,或者您也可以使用新实例
    【解决方案2】:
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
    

    您要为图像显示此代码

    你应该寻找真正的路径

    查看答案:Get filepath and filename of selected gallery image in Android

    android get real path by Uri.getPath()

    【讨论】:

      【解决方案3】:

      在你的第一个片段中

      if (resultCode == RESULT_OK && requestCode == 100){
                     val bundle = Bundle()
                     bundle.putString("Image",your_image_path)
                  findNavController().navigate(R.id.your_destination_fragment, bundle)
      

      }

      在第二个片段中

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          arguments?.let {
                   image= it.getString("Image")
                   bitmp= MediaStore.Images.Media.getBitmap(
                          requireActivity().contentResolver,
                           Uri.parse(image)
      

      }

      并在 imageview 中设置您的位图

      imgBitmap.setImageBitmap(bmp)

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多