尝试使用官方Android文档中的代码简单地拍摄照片但获得例外

时间:2017-02-13 07:28:09

标签: android android-permissions android-camera-intent

我正在尝试从https://developer.android.com/training/camera/photobasics.html的官方教程中学习 不幸的是,我不能让代码工作,因为我修改代码我得到一个与“File image = File.createTempFile ...”行有关的错误,有时来自其他行,这取决于我如何修补。鉴于下面的代码,我得到以下错误:

 Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Pictures/JPEG_20170213_015919_1702296485.jpg
                      at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
                      at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
                      at aaa.com.sss.MaxweightActivity.dispatchTakePictureIntent(MaxweightActivity.java:353)

第353行是“FileProvider.getUriForFile”行。我已将targetsdk设置为22以避免Marshmallow权限问题。我的清单看起来像这样:

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

    <uses-feature android:name="android.hardware.camera"
        android:required="false" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MaxweightActivity"
            android:label="@string/title_activity_settings"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".SettingsActivity" />
        <activity android:name=".listMaxweightsActivity" />

        <activity android:name=".chartsActivity"></activity>



        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="aaa.com.sss.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths"></meta-data>
        </provider>
    </application>


</manifest>

和我的filepaths.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/aaa.com.sss/files/Pictures" />
</paths>

Java代码:

String mCurrentPhotoPath;
private static final int REQUEST_TAKE_PHOTO = 101;

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 =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    //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 =   image.getAbsolutePath();
    Log.d(TAG, " mCurrentPhotoPath " + mCurrentPhotoPath+  " exists "+image.exists());
    return image;
}


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(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.e(TAG, "dispatchTakePictureIntent   ", ex);
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Log.d(TAG, " photoFile " + photoFile.getAbsolutePath() +  " exists "+photoFile.exists());
          ///// LINE 353 is next /////////
            Uri photoURI = FileProvider.getUriForFile(this,
                    "aaa.com.sss.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
       // Bitmap imageBitmap = (Bitmap) extras.get("data");
       // mImageView.setImageBitmap(imageBitmap);
    }
}

public void onCamera(View view) {
    dispatchTakePictureIntent();
  /*  String type = "image*//*";
    String filename = "/myPhoto.jpg";
    String mediaPath = Environment.getExternalStorageDirectory() + filename;
    createInstagramIntent(type, mediaPath);*/
}

我满足于将照片保存到公共场所。我正在尝试制作一个可以打开相机的按钮,允许我拍照然后返回我的应用程序,然后将照片与我的应用程序数据库中的记录相关联,然后将图像分享给社交媒体来自我应用的文字数据。由于我不能让相机部件工作,我被卡住了。我究竟做错了什么?

顺便说一句,我知道同样的官方教程已被问过很多次了,我试过看了很多答案,但这个官方代码似乎有很多问题,所以我不确定该相信什么。我遇到的另一个错误是从相机返回时数据为空,一旦发现此异常,我可以担心这个数据。

由于

PS:我知道这可能与其他问题的问题相同,但是它可能与我针对sdk 22的目标不一致,而另一个可能针对23,因此可能遇到与此问题相关的问题新的权限模型。另一个问题仍然没有答案,我尝试了那里建议的解决方案无济于事。几条日志消息(添加到我上面发布的代码中)显示至少创建了临时文件:

SS_MaxweightActivity:  path true:
D/SS_MaxweightActivity:  mCurrentPhotoPath /storage/emulated/0/Pictures/JPEG_20170213_160915_649829663.jpg exists true
D/SS_MaxweightActivity:  photoFile /storage/emulated/0/Pictures/JPEG_20170213_160915_649829663.jpg exists true

更新:新代码(下面,取自2或3个来源)似乎适用于我的用途,但跳过FileProvider的东西,现在要针对各种Android版本进行测试 - 这个代码是否适用于Android 7.0设备?

    static final int SHARETO = 2;
    static final int TAKE_PICTURE = 1;
    private Uri imageUri;



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, " onActivityResult 1 :"  );
        switch (requestCode) {
            case TAKE_PICTURE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = imageUri;
                    String type = "image/*";
                    createInstagramIntent(type  );
                }
                Log.d(TAG, " onActivityResult 2 :"  );
                break;
            case SHARETO:
                Log.d(TAG, " onActivityResult 3 :"  );
                break;
        }


        Log.d(TAG, " onActivityResult end :"  );
    }

    public void onCamera(View view) {
        Log.d(TAG, " onCamera 1 :"  );
        imageUri = null;
        takePhoto(view) ;

        Log.d(TAG, " onCamera 2 :"  );
    }



    public void takePhoto(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + ".jpg";
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File photo = new File(path,  imageFileName);
        imageUri = Uri.fromFile(photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

        Log.d(TAG, "takePhoto imageUri path  :"+imageUri.getPath()  );
        startActivityForResult(intent, TAKE_PICTURE);
    }


    private void createInstagramIntent(String type){ //, String mediaPath
        // Create the new Intent using the 'Send' action.
        Intent share = new Intent(Intent.ACTION_SEND);
        // Set the MIME type
        share.setType(type);
        //  copy lift data to clipboard

        // Add the URI to the Intent.
        share.putExtra(Intent.EXTRA_STREAM, imageUri);
        // Broadcast the Intent.
        startActivityForResult(Intent.createChooser(share, getString(R.string.shareto)), SHARETO);
    }

0 个答案:

没有答案