如何开始一个新的 activity 传递图像 uri
How to start a new activity passing an image uri
我有一个 activity 带有一个按钮,可以打开一个对话框,允许在拍照或从图库中选择图片之间进行选择编码如下:
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();
}
然后它运行以下意图来执行 selected 操作:
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);
}
}
}
现在我想开始一个新的 activity 来显示图片并允许修改这张图片所以我虽然我会在“onActivityResult”中传递 URI 但我不知道如何去做。
到目前为止它没有崩溃,我可以 select 图像或拍照,但当然当我这样做时,它只会回到活动中,除了希望将图像存储在文件。
我的问题:
有没有更好的方法来继续将图像传递给新的 activity 同时保持其完整质量?
如果没有,我如何传递此 URI 并在新的 activity 中检索图像?
我的图像是否实际存储在我创建的文件中?
这是我的 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);
}
我创建了这个新的 activity,它只有一个 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”之外的其他方法设置图像吗?
实际上你有两个选择:
- 如果您想要 small-sized 图片,您可以通过解码位图在
onActivityResult
回调中获取它
- 由于您传递的是 uri,因此使用
EXTRA_OUTPUT
键,MediaStore
会将文件保存到该位置。所以在 onActivityResult
回调中,您只需检查消息是否为 ok
以及请求代码是否对应,之后您可以使用您通过 [=11= 传入的 URI 从文件中读取]键
在 onActivtyResult 检查中,如果 URI 不为空,则将其传递给您的新 activity
Intent intent = Intent(this, your activity)
intent.putExtra("uri", uri)
startActivity(intent)
然后在(您的新 activity)中使用这些行来获取 Uri
Bitmap photo= BitmapFactory.decodeFile(intent.getStringExtra("uri"))
imageView.setImageBitmap(photo)
注意:将 uri 作为字符串传递
我有一个 activity 带有一个按钮,可以打开一个对话框,允许在拍照或从图库中选择图片之间进行选择编码如下:
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();
}
然后它运行以下意图来执行 selected 操作:
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);
}
}
}
现在我想开始一个新的 activity 来显示图片并允许修改这张图片所以我虽然我会在“onActivityResult”中传递 URI 但我不知道如何去做。
到目前为止它没有崩溃,我可以 select 图像或拍照,但当然当我这样做时,它只会回到活动中,除了希望将图像存储在文件。
我的问题: 有没有更好的方法来继续将图像传递给新的 activity 同时保持其完整质量? 如果没有,我如何传递此 URI 并在新的 activity 中检索图像? 我的图像是否实际存储在我创建的文件中?
这是我的 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);
}
我创建了这个新的 activity,它只有一个 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”之外的其他方法设置图像吗?
实际上你有两个选择:
- 如果您想要 small-sized 图片,您可以通过解码位图在
onActivityResult
回调中获取它 - 由于您传递的是 uri,因此使用
EXTRA_OUTPUT
键,MediaStore
会将文件保存到该位置。所以在onActivityResult
回调中,您只需检查消息是否为ok
以及请求代码是否对应,之后您可以使用您通过 [=11= 传入的 URI 从文件中读取]键
在 onActivtyResult 检查中,如果 URI 不为空,则将其传递给您的新 activity
Intent intent = Intent(this, your activity)
intent.putExtra("uri", uri)
startActivity(intent)
然后在(您的新 activity)中使用这些行来获取 Uri
Bitmap photo= BitmapFactory.decodeFile(intent.getStringExtra("uri"))
imageView.setImageBitmap(photo)
注意:将 uri 作为字符串传递