Android:保存到getExternalPublicStorageDirectory并显示在图库

时间:2017-08-29 23:01:42

标签: android android-intent camera gallery

所以我正在关注Android教程Capture(带摄像头意图)简单

在教程中,它专门用于在创建图像"文件"时保存图片时使用getExternalStoragePublicDirectory调用,但在他们的示例中,他们使用getExternalStorageDirectory。基本上它是"按照我的说法做不像我做的那样"例。

所以我尝试使用getExternalStoragePublicDirectory出现所有类型的错误。我最终让它工作,我可以通过ADB shell看到这些文件确实存在。对于我的生活,我无法让它们出现在画廊中。使用媒体扫描仪连接扫描文件似乎没有办法。有谁知道如何让文件显示在图库中?

代码段:

    private File createImageFile() throws IOException {    // Create an image file name
        String timeStamp = new 
        SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);


        // if the file does not exist, then create the file.
        if(!pictureDir.exists())
        {
            pictureDir.mkdirs();
        }

        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                pictureDir      /* directory */
        );

        boolean exists = image.exists();

        this.currentPhotoPath = image.getAbsolutePath();

        // Save a file: path for use with ACTION_VIEW intents
        return image;
    }

    private void dispatchTakePictureIntent()
    {

    verifyStoragePermissions(this);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // i believe this is checking if the activity exists.
        if(takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            File imageFile = null;

            try
            {
                imageFile = createImageFile();
            }
            catch (IOException e)
            {
                Log.i("sf", e.getMessage());
                Log.i("sf",e.getCause().getMessage());
            }

            if(imageFile != null)
            {
                imageFile.toURI();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFile.toURI());
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

       private void addGalleryPic() throws IOException {
           Intent mediaScanIntent = new 
           Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
           File f = new File(this.currentPhotoPath);

           if(f.exists())
           {
               Log.i("sdf", f.getAbsolutePath() + " exists");

           }

           MediaScannerConnection.scanFile(this,
                   new String[]{currentPhotoPath}, null,
                   new MediaScannerConnection.OnScanCompletedListener() {
                       public void onScanCompleted(String path, Uri uri) {
                           String p = uri.getPath();
                           Log.i("ExternalStorage", "Scanned " + path + ":");
                           Log.i("ExternalStorage", "-> uri=" + uri);
                       }
                   });
       }

1 个答案:

答案 0 :(得分:0)

尝试将其保存到内部存储空间,因为手机应该会在内部存储空间中提取任何照片。

希望它有所帮助!