如何启动传递图像uri的新活动



我有一个活动,它有一个按钮,可以打开一个对话框,在拍照或从图库中选择图片之间进行选择,编码如下:

private void showDialogToSelectAFile() {
MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this);
dialog.setTitle("Select a file or take a picture")
.setMessage("Choose one of the options")
.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
}).setPositiveButton("Take a picture", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
openCameraToTakePictureIntent();
}
}).setNegativeButton("Select a file from your gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
openGalleryIntent();
}
}).show();
}

然后,它运行以下意图来执行所选操作:

private void openCameraToTakePictureIntent() {
Log.d(TAG, "Method for Intent Camera started");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.emergence.domain.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}

现在我想开始一个新的活动,它将显示图片并允许对此图片进行修改;onActivityResult";但我不知道怎么做。

到目前为止,它还没有崩溃,我可以选择图像或拍照,但当然,当我这样做时,它只会回到活动中,除了希望将图像存储在文件中之外,什么都不做。

我的问题:是否有更好的方法将图像传递给新活动,同时保持其完整质量?如果没有,我如何传递这个URI并在新活动中检索图像?我的图像真的存储在我创建的文件中吗?

这是我的createImageFile函数,以防它相关:

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,  /* prefix */
".jpg",         /* suffix */
storageDir      /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}

感谢您的帮助

编辑:

我更新了onActivityResult如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
Intent intent = new Intent(this, ModifyPictureActivity.class);
intent.putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoPath);
startActivity(intent);
}

我创建了这个新活动,它只有一个ImageView,我想在其中显示图片,但我找不到将图像设置为Extras的正确方法。到目前为止,我有这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_picture);
Intent intent = getIntent();
intent.getExtras();
image = findViewById(R.id.image);
image.setImageURI(???);
}

我是否应该尝试用另一种方法来设置图像;setImageURI";?

实际上您有两个选项:

  1. 如果你想要一个小尺寸的图像,你可以通过解码位图在onActivityResult回调中获得它
  2. 由于您使用EXTRA_OUTPUT键传递uri,MediaStore将把文件保存到该位置。因此,在onActivityResult回调中,您只需检查消息是否为ok,并且请求代码是否对应,然后您就可以从具有用EXTRA_OUTPUT键传入的URI的文件中读取

在onActivtyResult检查中,如果URI不为null,则将其传递给新的活动

Intent intent = Intent(this, your activity)
intent.putExtra("uri", uri)
startActivity(intent)

然后在(您的新活动(中使用这些行获取Uri

Bitmap photo= BitmapFactory.decodeFile(intent.getStringExtra("uri"))
imageView.setImageBitmap(photo)

注意:将uri作为字符串传递

最新更新