将位图转换为有效的Uri

问题描述 投票:0回答:1

我正在开发一个Android应用,用户可以从图库中选择图像,也可以从相机中捕获图像。当用户从图库中获取图像时,我会获得图像uri,然后我将此uri以字符串形式传递给其他活动。然后在下一个活动中,我将该字符串转换为uri,然后将uri转换为位图,并在imageview中设置图像位图。现在,当我从相机捕获图像时,会得到图像位图。现在,我想将此位图转换为有效的uri并传递到下一个活动

    if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            System.out.println("URLLL "+selectedImage);
            Log.v("PhotoActivity", "Captured image");

            //Create intent
            Intent intent = new Intent(MainActivity.this, FlagDisplayActivity.class);
            intent.putExtra("URI", selectedImage.toString());
            //Start Flag Display activity
            startActivity(intent);
            Log.v("PHOTO ACTIVITY", " uri: " + selectedImage);
        }
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
        {

            Bitmap photo = (Bitmap) data.getExtras().get("data");

            Intent intent = new Intent(MainActivity.this, FlagDisplayActivity.class);
            intent.putExtra("URI", photo);
            //Start Flag Display activity
            startActivity(intent);

}

这是我下次活动中如何获得uri的方法

  String imageUriString=getIntent().getStringExtra("URI");
        final Uri selectedImage=Uri.parse(imageUriString);

然后像这样将uri转换为位图

Bitmap bitmap = null;
    try {
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),
                selectedImage);
    } catch (IOException e) {
        e.printStackTrace();
        bitmap=StringToBitMap(imageUriString);
    }

我的主要目标是将位图转换为uri

android bitmap
1个回答
1
投票

提供图像路径,它将为您提供图像uri

String realPath = ImageFilePath.getPath(SignupActivity.this, data.getData());

String picturePath = getPath( getActivity( ).getApplicationContext( ), selectedImageUri ); // Put here data.getData(); instead of selectedImageUri
File f = new File(PATH);
Uri yourUri = Uri.fromFile(f);
© www.soinside.com 2019 - 2024. All rights reserved.