简体   繁体   中英

Launching camera and creating a name for the picture is returning null Intent to onActivityResult

My problem is in this line:

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

when i comment it the camera does not return null intent to onActivityResult but when I uncomment it, it sends null to Intent data,but its saving the file with the name i specified.

Here is the code:

Start Camera:

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);
}

Create File:

 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 code:

 @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();


    }

}

Manifest Code:

<?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>

检查是否写入外部存储权限,实现动态权限模型,否则在代码中看不到任何错误

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM