2016-10-15 13 views
0

私はこのコードを使用して、写真を撮影するカメラを実行しています。すべてのステップでHEREステップから取られている(フルサイズのカメラオプション)カメラインテントが失敗する

private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // Ensure that there's a camera activity to handle the intent 
     if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { 

      File photoFile = null; 
      try { 
       photoFile = createImageFile(); 
      } catch (IOException ex) { 
       // Error occurred while creating the File 

      } 
      // Continue only if the File was successfully created 
      if (photoFile != null) { 
       Uri photoURI = FileProvider.getUriForFile(getActivity(), "com.example.android.fileprovider", photoFile); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
       startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
      } 
     } 
    } 

    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 = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
      File image = File.createTempFile(imageFileName, ".jpg",storageDir); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
     Toast.makeText(getActivity(), mCurrentPhotoPath, Toast.LENGTH_SHORT).show(); 
     return image; 
    } 

これは私のManifestファイルです:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.terrormachine.swipeapp"> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.example.android.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths"> 
     </meta-data> 
        </provider> 

<activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

と私のパスのxml:

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
        <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /> 
</paths> 

と私は取得していますこのエラー:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.terrormachine.swipeapp/files/Pictures/JPEG_20161015_211933_-593154643.jpg 

ファイル "shell"がそこにあるので(この場所で)少し驚いた。 私はTHISスレッドをfountしかし、私は事を理解できない...それを人間的に説明することができますか?どんな解決策も大歓迎です!その重要なプロジェクトと私はできるだけ多くを完了する必要があり、これは大きなストップです。

+1

サポートライブラリの最新バージョンに更新します。次に、 'external-path'の代わりに' external-files-path'を使い、 'path'を' Pictures'だけで置き換えます。また、[Uri]に[許可を与える必要があります](https://commonsware.com/blog/2016/08/31/granting-permissions-uri-intent-extra.html)、そうでなければカメラアプリ「ウリ」を使うことはできません。 'FileProvider'と' ACTION_IMAGE_CAPTURE'を使用する例については、[私はこのサンプルアプリケーション](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider)を参照してください。ストレージ。 – CommonsWare

答えて

1

files_path.xmlには、開発者サイトで説明されているように、com.example.package.nameをアプリパッケージ名に置き換える必要があります。

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="my_images" path="Android/data/com.example.terrormachine.swipeapp/files/Pictures" /> 
</paths> 

また、AndroidManifest.xmlファイルにカメラ権限を追加します。

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" 
       android:required="true" /> 
+0

ありがとうございました。人生の節約と迅速な反応! – nephilimrising

1

私は、あなたが「使用-機能のアンドロイド:名=」欠けていると思うandroid.hardware.camera「アンドロイド:必要= 『』」本当のマニフェストでは。

関連する問題