簡體   English   中英

如何將圖像從一個片段發送到另一個片段?

[英]How to send image from one fragment to another?

我讀了這些,但錯誤仍然存在:

如何將所選圖像從一個片段傳遞到另一個片段

將圖像從一個片段傳遞到另一個片段並在該片段中顯示圖像

我創建了一個活動,它將數據和圖像從一個片段傳輸到另一個片段,數據解析成功但是當我發送圖像時它說

2021-05-16 04:08:52.127 26026-26026/com.example.mcqapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/image:11600: open failed: ENOENT (No such file or目錄)

這是我的片段代碼

if (imageView.getVisibility() == View.VISIBLE) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {

        filePath = data.getData();
        path_New = filePath.getPath();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),filePath);
            imageView.setImageBitmap(bitmap);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
bundle.putString("image",path_New);
second mfragment=new second();
mfragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment1, mfragment).commit();

第二個片段中的代碼接收圖像:

Bundle bundle = getArguments();
String imagePath = bundle.getString("image");


Bitmap bitmap  = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);

好吧,我解決了,我將 uri 轉換為字符串,然后在第二個片段中我將字符串轉換為

urifilePath = data.getData();
String path = filePath.toString();
bundle.putString("image",path);

第二個片段

String imagePath = bundle.getString("image");
Uri myUri = Uri.parse(imagePath);
imageView.setImageURI(myUri);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM