簡體   English   中英

如何將圖片庫從片段傳遞到另一個活動?

[英]How to passing Image Gallery from fragment into another activity?

將圖像從我的畫廊傳遞到另一個活動時遇到問題。 選項活動中的相機片段UI 中的 UI

UI 的屏幕截圖在上面。 我已經做了拍照(打開相機)並將圖像傳遞給 UI 2 及其作品,但是從打開的畫廊它不能,怎么做?

這是我在CameraFragment 中的代碼

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_kamera, container, false);
    imageView1 = view.findViewById(R.id.imageView1);
    show_graph = view.findViewById(R.id.show_graph);
    btnTakepic = view.findViewById(R.id.btnTakepic);
    Gofilter = view.findViewById(R.id.Gofilter);

btnTakepic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

Gofilter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), option.class);
            if(imageView1.getDrawable() == null)
            {
                startActivity(intent);
            }else {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();
                intent.putExtra("image", bitmap);
             // intent.putExtra("imageUri", imageUri.toString());
                startActivity(intent);
            }//  else {
             //   BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
              // Bitmap imageBitmap = bitmapDrawable.getBitmap();
              // intent.putExtra("image2", imageBitmap);
                // startActivity(intent);
               //    }

        }
    });

  return view;
}

 private ByteArrayInputStream convertDrawable(ImageView image2){
    BitmapDrawable bitmapDrawable = ((BitmapDrawable) image2.getDrawable());
    Bitmap bitmap = bitmapDrawable.getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
 // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    return new ByteArrayInputStream(imageInByte);
}

這是來自我的OptionActivity

public class option extends AppCompatActivity {

ImageView imageView;
Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_option);

    imageView = findViewById(R.id.imageView2);

    Intent intent = getIntent();
     //  Bundle extras = getIntent().getExtras();
    Bitmap bitmap = intent.getParcelableExtra("image");
     //  Bitmap imageUri1 = intent.getParcelableExtra("imageUri");
    if(bitmap!=null){
        imageView.setImageBitmap(bitmap);
    }   
          // else {
           //            Uri imageUri = Uri.parse(extras.getString("imageUri"));
           //            imageView.setImageURI(imageUri);
           //        }
            //        else if (getIntent().getExtras() != null) {
           //            imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
           //            imageView.setImageURI(imageUri);
          //        }


}
}

抱歉英語不好,我希望任何人都可以幫助我謝謝:)

你應該只傳遞照片的Uri 不是整個位圖。

  Uri getUri(Bitmap yourBitmapToPass) {
    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;
    File file = new File(path, "tempPhoto.jpg");
    try {
        fOut = new FileOutputStream(file);
        Bitmap pictureBitmap = yourBitmapToPass;
        pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.fromFile(file);
}


void open(Intent intent) {
    Uri myUri = Uri.parse(intent.getExtras().getString("imageUri"));
}

並以這種方式發送:

intent.putExtra("imageUri", uriToPass.toString());

我也建議使用緩存目錄

暫無
暫無

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

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