Android:捕获的图像未显示在图库中(Media Scanner意图不起作用)

hil*_*ard 3 java media android image android-intent

我有以下问题:我正在开发一个应用程序,用户可以在其中拍照(附加到帖子),图片保存到外部存储.我希望这张照片也出现在图片库中,我正在使用Media Scanner意图,但它似乎不起作用.我在编写代码时遵循官方Android开发人员指南,所以我不知道出了什么问题.

部分代码:

用于捕获图像的意图:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

创建文件以保存图像:

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;
}
Run Code Online (Sandbox Code Playgroud)

在视图中显示图像:

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);
}
Run Code Online (Sandbox Code Playgroud)

广播媒体扫描仪,旨在使图像显示在图库中:

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);
}
Run Code Online (Sandbox Code Playgroud)

返回Image Capture意图后运行的代码:

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

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

        galleryAddPic();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

sak*_*kiM 8

这取决于该设备如何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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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


小智 2

在 Android 中,如果您从应用程序捕获或添加 SD 卡等中的图像。

有时它不会在画廊中显示一段时间。

如果您捕获并等待一段时间直到同步过程完成,您将能够看到。

现场你可能看不到。