2016-08-12 181 views
0

我在保存照片时出现问题,在应用程序目录中(如示例whatsapp)直接在我的应用程序上拍照时出现问题。应用程序拍摄的照片不保存在图库中

在我清单我已经设置这些许可:

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.myapp.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true" 
     android:readPermission="com.myapp.READ"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths"/> 
    </provider> 

</application> 

在我的活动,我有:

public void showCamera(View v){ 
     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     if (intent.resolveActivity(getPackageManager()) != null) { 
      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 

      } 

      if (photoFile != null) { 
       Uri photoURI = FileProvider.getUriForFile(this, 
         "com.myapp.fileprovider", 
         photoFile); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 

       startActivityForResult(intent, REQUEST_TAKE_PHOTO); 
       galleryAddPic(); 
      } 
     } 
    } 

    String mCurrentPhotoPath; 


    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); 
    } 
    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 = 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 = "file:" + image.getAbsolutePath(); 
     return image; 
    } 

在我的文件路径资源:

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
<external-path name="my_images" path="Android/data/com.myapp/files/Pictures" /> 

它打开相机,拍摄照片并返回我的活动,但我无法在任何地方找到它,所以看起来不保存照片。

回答

0

我找不到它的任何地方

你没有说明你是如何寻找它。使用adb shell可获得文件系统级别的外观,看看您的设备或模拟器上的内容。

现在,您的ACTION_MEDIA_SCANNER_SCAN_FILE将无法​​正常工作,因为您使用的路径无效。文件系统路径不以file:开头。我建议完全摆脱mCurrentPhotoPath,并坚持使用createImageFile()返回的文件,并使用ACTION_MEDIA_SCANNER_SCAN_FILE请求(或使用MediaScannerConnection.scanFile()而不是ACTION_MEDIA_SCANNER_SCAN_FILE)。

另外,正如Hardik指出的那样,您过早打电话给galleryAddPic()。等待拍摄照片,通过onActivityResult()调用galleryAddPic()。虽然文件应存在于您现在调用galleryAddPic()时的位置,但它不会具有正确的详细信息(例如文件大小)。这可能会混淆用户。你最好等待,直到图片准备好,然后要求它被索引。

+0

好吧,我把'galleryAddPic()'放在'onActivityResult'的Override上,但没有任何改变。 我有'CreateImageFile()'和'galleryAddPic()'我有'Uri contentUri = Uri.fromFile(photoFile);' 返回的文件可能是sendBroadcast的问题? – LorenzoBerti

+0

@LorenzoBerti:“可能是sendBroadcast有问题吗?” - 使用'adb shell'来查看图像是否存在,你要求它被保存。如果完整的照片存在,那么媒体扫描逻辑会出现问题。如果文件存在,但是它是空的,那么图像捕获逻辑就会出错('createTempFile()'会创建一个空文件)。如果该文件不在那里,那么你的代码没有被运行。 – CommonsWare

+0

Thankyou,我在Android/data/com.myapp/files /图片中看到照片,但重新启动后,但不在画廊中,如何刷新媒体并将其放入媒体库中? – LorenzoBerti

1

如果一切正常,然后将galleryAddPic()方法放入onActivityResult中,您可以在其中收集捕获的图片。

相关问题