gpt4 book ai didi

java - Android:捕获的图像未显示在图库中(媒体扫描器 Intent 不起作用)

转载 作者:太空狗 更新时间:2023-10-29 15:45:38 25 4
gpt4 key购买 nike

我有以下问题:我正在开发一个应用程序,用户可以在其中拍照(以附加到帖子中)并将图片保存到外部存储。我希望这张照片也显示在图片库中,为此我正在使用媒体扫描仪,但它似乎不起作用。我在编写代码时遵循的是官方 Android 开发人员指南,所以我不知道出了什么问题。

我的部分代码:

捕获图像的 Intent :

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().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
Toast.makeText(getActivity(), ex.getMessage(), Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}

创建一个文件来保存图像:

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 = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}

在 View 中显示图像:

private void setPic() {
// Get the dimensions of the View
int targetW = 60;
int targetH = 100;

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
img_added.setImageBitmap(bitmap);
}

广播一个 Media Scanner Intent 使图像显示在图库中:

private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
}

图像捕获 Intent 返回后运行的代码:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
setPic();

galleryAddPic();
}
}

我也曾尝试使用 Intent.ACTION_MEDIA_MOUNTED 而不是 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,但在那种情况下我得到了“权限被拒绝”的错误。当我记录传递给 Intent 的 URI 时,我得到 file:///storage/emulated/0/Pictures/JPEG_20150803_104122_-1534770215.jpg,这应该没问题。除此以外,其他一切正常(捕获图像,将其保存到外部存储并在 View 中显示),所以我真的不知道出了什么问题。有人有什么主意吗?提前致谢!

最佳答案

这取决于此设备Gallery 的实现方式。流行的照片应用接收 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 广播,但有些只听 Android 媒体数据库。

或者,您可以同时手动将图像插入 MediaStore

public final void notifyMediaStoreScanner(final File file) {
try {
MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
file.getAbsolutePath(), file.getName(), null);
mContext.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

此外,请确保您的照片不在private 文件夹中,例如内部存储或使用Context.MODE_PRIVATE 写入。否则其他应用将无权访问此文件。

关于java - Android:捕获的图像未显示在图库中(媒体扫描器 Intent 不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com