使用文件提供程序将相机照片从片段保存到图库

时间:2017-03-24 16:29:14

标签: java android android-fragments

我正在关注如何使用相机将图片保存到图库中的Googles Official文档。

他们希望您使用getExternalFilesDir创建文件。

    String mCurrentPhotoPath;
    private File createImageFile() throws IOException {
        // Create an image file name

         String imageFileName = "JPEG_" + UUID.randomUUID();
        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
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

mCurrentPhotoPath 等于/storage/emulated/0/Android/data/com.mycompany.myapp/files/Pictures/JPEG_22fda6f2-dad9-4dd9-b327-c1130c8df0eb187766077.jpg

但是在下一节,最重要的部分,将照片添加到图库

他们说:

  

如果您将照片保存到提供的目录中   getExternalFilesDir(),媒体扫描程序无法访问文件   因为它们对您的应用是私密的。

他们使用的确切方法getExternalFilesDir()。 : - (

所以我也看了documentation。我还不太了解我需要使用哪种目录方法。我尝试了getFilesDir(),但它不喜欢Environment.DIRECTORY_PICTURES

但他们没有提供使用他们的方法保存到画廊的方法。他们的代码片段不起作用

private void cameraIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(getActivity().getApplicationContext(), "com.mycompany.myapp.fileprovider", photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_CODE_CAPUTURE_IMAGE);
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CAPUTURE_IMAGE && resultCode == Activity.RESULT_OK) {
                galleryAddPic();

        }
        }

    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);
        this.sendBroadcast(mediaScanIntent);
    }

所以我的应用将图片保存到图库。我根本没看到它在哪里保存它。

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

当您向Android的文件系统添加文件时,MedaScanner不会自动获取这些文件,Android也会在重新启动时运行完整的媒体扫描。问题是全扫描需要很长时间。

一种解决方案是使用静态scanFile()方法。如果您只是想知道文件的添加时间,可以使用MediaScannerConnection的静态方法 scanFile()MediaScannerConnection.OnScanCompletedListener。静态方法 scanFile()命名错误,因为它实际上需要一个路径数组,因此可以用于一次添加多个文件而不仅仅是一个,但它仍然可以实现我们想要的。 以下是使用此方法的方法:

MediaScannerConnection.scanFile(
  getApplicationContext(),
  new String[]{file.getAbsolutePath()},
  null,
  new OnScanCompletedListener() {
     @Override
     public void onScanCompleted(String path, Uri uri) {
        Log.v("grokkingandroid",
              "file " + path + " was scanned seccessfully: " + uri);
     }
  });

以下信息是静态 scanFile()方法的参数。

  • 上下文:应用程序上下文
  • 路径:包含要添加的文件路径的String数组
  • mimeTypes :包含文件的mime类型的String数组
  • 回调:扫描完成时通知的MediaScannerConnection.OnScanCompletedListener

OnScanCompletedListener 本身必须实现 onScanCompleted()方法。此方法获取作为参数传入的 MediaStore.Files 提供程序的文件名和URI。

我希望这会有所帮助。

相关问题