相机意图失败

时间:2016-10-15 18:30:45

标签: android android-intent camera

我正在使用此代码运行相机以拍摄照片。全部从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线程,但我无法理解一件事......你能解释它是人类的吗?欢迎任何解决方案!这是一个重要的项目,我需要尽可能地完成,这是一个很大的停止。

2 个答案:

答案 0 :(得分:1)

在您的files_path.xml中,您需要将 com.example.package.name 替换为您的应用包名称,如开发者网站上所述。

CustomerID | Product
--------------------
         1 | ProductA
         2 | ProductA
         3 | ProductA
         6 | ProductA
         7 | ProductA

还要在AndroidManifest.xml文件中添加相机权限。

CREATE TABLE #TempTable 
(
     CustomerID INT,
     Product    VARCHAR(50)
)

INSERT INTO #TempTable (CustomerID, Product)
VALUES
  ('1', 'ProductA'),
  ('2', 'ProductA'),
  ('3', 'ProductA'),
  ('4', 'ProductA'),
  ('4', 'ProductB'),
  ('6', 'ProductA'),
  ('7', 'ProductA');

答案 1 :(得分:1)

我认为你缺少&#34;使用功能android:name =&#34; android.hardware.camera&#34;机器人:需要=&#34;真&#34;&#34;在你的清单中。