Matisse: 通过图库内的拍照功能拍的照片在图库中无法找到

Created on 29 Oct 2018  ·  14Comments  ·  Source: zhihu/Matisse

Describe the bug
通过图库内的拍照功能拍的照片在图库中无法找到,直接用手机相机拍摄的照片是有的,只有通过图库拍摄的照片找不到。

To Reproduce

   Matisse.from(this)
                        .choose(MimeType.ofAll())
                        .countable(true)
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .gridExpectedSize(250) 
                        .thumbnailScale(0.85f)
                        .maxSelectable(1)
                        .captureStrategy(new CaptureStrategy(
                                true, "*****.fileprovider"))
                        .imageEngine(new MyGlideEngine())
                        .theme(R.style.Matisse_Zhihu)
                        .forResult(NINE_GRID_REQUEST_CODE_CHOOSE);

      <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public"></meta-data>
        </provider>


<?xml version="1.0" encoding="utf-8"?>
<rec>
    <external-path
        name="my_images"
        path="Pictures"/>
</rec>

Expected behavior
选择图库中的拍照功能,完成后再次进入图库,刚刚拍的照片不管在哪都找不到。是不是没有保存或者在完成后就被删除了?

Screenshots

Smartphone (please complete the following information):

Additional context

Most helpful comment

在MatisseActivity的onActivityResult方法中if(requestCode == REQUEST_CODE_CAPTURE){}判断的最后添加广播扫描文件夹
` if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
MatisseActivity.this.revokeUriPermission(contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        File storageDir;
        if (mSpec.captureStrategy.isPublic) {
            storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            if (!storageDir.exists()) storageDir.mkdirs();
        } else {
            storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri storageUri = Uri.fromFile(storageDir);
        mediaScanIntent.setData(storageUri);
        sendBroadcast(mediaScanIntent);
        finish();
    }`

All 14 comments

I got this problem as well. Device:MI6, OS : Android8.0.0, Version : com.zhihu.android:matisse:0.5.1

Photo taken from matisse was saved into internal/external file directory which is ignored by system gallery.

try this:

File file = new File(filePath);
if (!file.exists()) {
return;
}
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);

try this:

File file = new File(filePath);
if (!file.exists()) {
return;
}
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);

it is not work

is it resolved?

Describe the bug
通过图库内的拍照功能拍的照片在图库中无法找到,直接用手机相机拍摄的照片是有的,只有通过图库拍摄的照片找不到。

To Reproduce

   Matisse.from(this)
                        .choose(MimeType.ofAll())
                        .countable(true)
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .gridExpectedSize(250) 
                        .thumbnailScale(0.85f)
                        .maxSelectable(1)
                        .captureStrategy(new CaptureStrategy(
                                true, "*****.fileprovider"))
                        .imageEngine(new MyGlideEngine())
                        .theme(R.style.Matisse_Zhihu)
                        .forResult(NINE_GRID_REQUEST_CODE_CHOOSE);

      <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public"></meta-data>
        </provider>


<?xml version="1.0" encoding="utf-8"?>
<rec>
    <external-path
        name="my_images"
        path="Pictures"/>
</rec>

Expected behavior
选择图库中的拍照功能,完成后再次进入图库,刚刚拍的照片不管在哪都找不到。是不是没有保存或者在完成后就被删除了?

Screenshots

Smartphone (please complete the following information):

Additional context

你好 我也遇到了这个问题 调试了很久了 还没解决 请问你解决了么?

解决了: 下面是我的办法
in MediaStoreCompat.java
dispatchCaptureIntent();
try to add the code :
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mCurrentPhotoUri = Uri.fromFile(photoFile);
} else {
mCurrentPhotoUri = FileProvider.getUriForFile(context, "com.xxx.xxx.xxx" + ".provider", photoFile);
Date date = new Date();
// Create an image file name
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = String.format("scv%s", timeStamp);
// 兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, imageFileName);
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, photoFile.getName());
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.Images.Media.DATA, mCurrentPhotoPath);
long time = date.getTime() / 1000;
contentValues.put(MediaStore.MediaColumns.DATE_ADDED, time);
contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, time);
if (photoFile.exists()) {
contentValues.put(MediaStore.Images.Media.SIZE, 图片大小);//
}
mCurrentPhotoUri = mContext.get().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
}
try {
MediaStore.Images.Media.insertImage(mContext.get().getContentResolver(), mCurrentPhotoPath, "", "");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

使用下面方法把照片插入到系统即可:

public static String insertImageToGallery(Context context, File file) {
       //把文件插入到系统图库
        String s="";
        try {
            s = MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), file.getName(), null);
            Log.d("FileUtils", s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
        return s;
    }

//知乎选择器拍照默认存储在Pictures文件夹,判断此文件夹是否存在。Pictures是有个android系统没有这个文件夹或者可能被用户主动删除
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!file.exists()) {
try {
file.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
只需在打开相册前加上这些代码就可以

解决了: 下面是我的办法
in MediaStoreCompat.java
dispatchCaptureIntent();
try to add the code :
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mCurrentPhotoUri = Uri.fromFile(photoFile);
} else {
mCurrentPhotoUri = FileProvider.getUriForFile(context, "com.xxx.xxx.xxx" + ".provider", photoFile);
Date date = new Date();
// Create an image file name
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = String.format("scv%s", timeStamp);
// 兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, imageFileName);
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, photoFile.getName());
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.Images.Media.DATA, mCurrentPhotoPath);
long time = date.getTime() / 1000;
contentValues.put(MediaStore.MediaColumns.DATE_ADDED, time);
contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, time);
if (photoFile.exists()) {
contentValues.put(MediaStore.Images.Media.SIZE, 图片大小);//
}
mCurrentPhotoUri = mContext.get().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
}
try {
MediaStore.Images.Media.insertImage(mContext.get().getContentResolver(), mCurrentPhotoPath, "", "");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

正解,亲测可行,多谢!

mContext.get().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

直接改源码吗

在MatisseActivity的onActivityResult方法中if(requestCode == REQUEST_CODE_CAPTURE){}判断的最后添加广播扫描文件夹
` if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
MatisseActivity.this.revokeUriPermission(contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        File storageDir;
        if (mSpec.captureStrategy.isPublic) {
            storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            if (!storageDir.exists()) storageDir.mkdirs();
        } else {
            storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri storageUri = Uri.fromFile(storageDir);
        mediaScanIntent.setData(storageUri);
        sendBroadcast(mediaScanIntent);
        finish();
    }`

I have the same problem !!

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file:"+path)));
或者更狠的:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile())));

在MatisseActivity的onActivityResult方法中if(requestCode == REQUEST_CODE_CAPTURE){}判断的最后添加广播扫描文件夹
` if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
MatisseActivity.this.revokeUriPermission(contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        File storageDir;
        if (mSpec.captureStrategy.isPublic) {
            storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            if (!storageDir.exists()) storageDir.mkdirs();
        } else {
            storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri storageUri = Uri.fromFile(storageDir);
        mediaScanIntent.setData(storageUri);
        sendBroadcast(mediaScanIntent);
        finish();
    }`

thanks a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Alastor111 picture Alastor111  ·  6Comments

meiniepan picture meiniepan  ·  7Comments

Freedomhxb picture Freedomhxb  ·  4Comments

hugoFather picture hugoFather  ·  5Comments

Mahan3340 picture Mahan3340  ·  4Comments