启动相机并为图片创建名称将返回null Intent to onActivityResult

时间:2018-04-01 01:27:13

标签: android android-intent android-camera uri onactivityresult

我的问题在于这一行:

pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());

当我对它进行评论时,相机不会向onActivityResult返回null意图,但是当我取消注释它时,它会向Intent数据发送null,但是它使用我指定的名称保存文件。

以下是代码:

启动相机:

private void invokeCamera() {

    // get a file reference
    Uri pictureUri;

        pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // tell the camera where to save the image.
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

    // tell the camera to request WRITE permission.
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

创建文件:

 private File createImageFile() {

    File imageFile;

        // the public picture director
        File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            String IMGname = Ti.getText().toString() + IMGCounter;

            // put together the directory and the timestamp to make a unique image location.
            imageFile = new File(picturesDirectory,  IMGname + ".jpg");

            Toast.makeText(this,  IMGname + ".jpg", Toast.LENGTH_LONG).show();

    return imageFile;

}

onActivityResult代码:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode ==  RESULT_OK && data != null){

        if(requestCode == REQUEST_IMAGE_CAPTURE) {

            Toast.makeText(this, "Reached REq", Toast.LENGTH_SHORT).show();
           // AddImgToView(data);

        }else if(requestCode == IMAGE_GALLERY_REQUEST){

            AddImgToView(data);

        }



    }else{

        Toast.makeText(this, "Op 2"+data, Toast.LENGTH_SHORT).show();


    }

}

清单代码:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<!--Required Permissions-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <provider
        android:authorities="company.com.retrofit.provider"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>

    </provider>


    <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=".InsertData"></activity>
</application>

2 个答案:

答案 0 :(得分:0)

检查写入外部存储权限,实现动态权限模型,否则我看到代码没有错误

答案 1 :(得分:0)

我发现默认的Android相机会在您发送缩略图时发送数据,但是当您使用EXTRA_OUTPUT时,它会发送空值,我必须使用我指定的URI。