2016-09-08 29 views
3

我用下面的鏈接集成攝像頭在我的應用程序打開相機並使用FileProvider無法保存圖像。收到錯誤:W/ContextImpl:無法保證目錄:

https://developer.android.com/training/camera/photobasics.html#TaskScalePhoto

我也跟着每一步,但我得到以下錯誤,每當試圖把圖片來源攝像頭和一個對話框也會彈出顯示

錯誤 「不幸的是,相機已經停止」:

W/ContextImpl: Failed to ensure directory: /storage/extSdCard/Android/data/com.example.daf.formvalidator/files/Pictures

E/OpenGLRenderer: SFEffectCache:clear(), mSize = 0

什麼奇怪,我在下面的代碼返回(從上面的鏈接所)在storageDir 的價值和上面的錯誤「/模擬/ 0」表示「extSdCard」

// returns /storage/emulated/0/Android/data/com.example.daf.formvalidator/files/Pictures/

File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES); 

我想要什麼?

-open camera app。

-store拍照到應用程序的私人存儲正如鏈接中所述。

-want使用FileProvider

我也試圖與諸如意圖設置標誌:

takePictureIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

我無法找到問題,並花了差不多1天發現的問題。 請提供任何形式的幫助。這將不勝感激。

爲什麼「storageDir的值」和錯誤信息有區別?

如果問題不明確,請建議...

回答

0

嘗試將此權限添加到您的清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

已經嘗試過這種 –

0

如果您正在使用從谷歌開發的例子試圖把你的文件自定義目錄:

String mCurrentPhotoPath; 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File externalStorageDirectory = Environment.getExternalStorageDirectory(); 
     File directory = new File(externalStorageDirectory.getAbsolutePath() + "/yourappname"); 
     //create directory if not exist 
     if (!directory.isDirectory()) { 
      directory.mkdirs(); 
     } 
    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; 
} 
+0

但我要像使用FileProvider存儲在應用的私人區。 –

0

所以,你想:

open camera app.

store taken picture to app's private storage as mentioned in the link.

want to use FileProvider

我想提到的第一件事是您發佈的錯誤根本不是錯誤。這是一個警告,你應該可以忽略它。我認爲這個警告意味着他們不知道目錄是否存在(這與我將在一秒鐘內討論的鏈接有關)。你能發佈你的異常的實際堆棧跟蹤嗎?聽起來不像這是你的問題。

接下來的事情從您的文章中不清楚的是,它聽起來像你混淆了私人存儲和SD卡。根據定義,SD卡上的文件不是專用存儲。請參閱此處以獲取有關您的選項的更長解釋:https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

具體而言,您要注意的是,您正在做的是保存到SD卡,SD卡根據定義不是內部(或私人)存儲。

至於之間的差「/存儲/模擬/ 0 /」和「/存儲/ SD卡/」,他們應該都指向同一目錄中。簡短的版本是,由於Android L,Android支持多個用戶。 「模擬/ 0」是一個只對主用戶的存儲目錄的鏈接。什麼是有趣,我這裏要說的是,通常當我聽到「模擬/ 0」,它在內部存儲的情況下,但我不明白爲什麼它不能鏈接到SD卡爲好。

相關問題