照片没有出现在画廊中

时间:2017-11-10 09:48:41

标签: android android-gallery android-fileprovider

我尝试在我的应用中拍摄并保存照片。 我写了清单中的fileprovider和路径:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="mypacage.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/path" />
            </provider>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images"  path="images"/>

</paths>

在我的活动中,我调用Fragment Dialog(cameraOrChoosePhoto Dialog),它有一个按钮,下一个打开相机的意图和其他动作为照片创建文件::

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_camera:
                Uri uri = generateUriPhoto();
                if(uri!=null) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
                                                          Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    getActivity().startActivityForResult(cameraIntent, SubItemDetail.CAMERA_RESULT);
                } else dismiss();
                break;
            case R.id.dialog_choose_photo:
                break;
        }
    }

    private Uri generateUriPhoto() {
        file = createNameFile(); 
        uri = null;
        if (file != null) {
                      String aut = getActivity().getPackageName()+".fileprovider";
                       uri = getUriForFile(getActivity(), aut, file);

                      getActivity().grantUriPermission(getActivity().getPackageName(), uri, 
                                                                              Intent.FLAG_GRANT_READ_URI_PERMISSION |
                                                                                Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        return uri;

    }

    public File getFile() {
        return file;
    }

    public Uri getUri() {  //  onActivityResult
        return uri;
    }

    private File createNameFile() {
        directory = new File(getActivity().getFilesDir(), "images");
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String name = "photo_" + System.currentTimeMillis();
        File file = null;
        try {
            file = File.createTempFile(
                    name,  /* prefix */
                    ".jpg",         /* suffix */
                    directory      /* directory */
            );
        } catch (IOException e) {
            e.printStackTrace();
            dismiss();
        }
        return file;
    }

接下来我的活动:

.........

private Uri uri

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        CameraOrChoosePhotoDialog cameraOrChoosePhotoDialog = (CameraOrChoosePhotoDialog)             getFragmentManager().findFragmentByTag("camera"); 

        uri = cameraOrChoosePhotoDialog.getUri();// get uri from dialogfargment

        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_RESULT) {

                 image.setImageURI(uri);
                addPicToGallery(); //  try  to put photo in gallery

                cameraOrChoosePhotoDialog.dismiss();  // close dialog
            }
        }
    }

    private void addPicToGallery() {

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri)); 
        mediaScanIntent.setData(uri));
         sendBroadcast(mediaScanIntent);


    }

但照片未显示在图库中

2 个答案:

答案 0 :(得分:0)

尝试将捕获的文件转换为.jpeg,.png等图像格式,然后将其存储到设备本地存储或SD卡中。

答案 1 :(得分:0)

替换

private void addPicToGallery() {

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri)); 
        mediaScanIntent.setData(uri));
         sendBroadcast(mediaScanIntent);


    }

private void addPicToGallery() {

            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri)); 
            mediaScanIntent.setData(Uri.fromFile(file)));
             sendBroadcast(mediaScanIntent);
        }

以某种方式来自FileProvider的初始uri与你从实际文件获得的uri不同:)