简体   繁体   中英

How to get image bitmap in OnActivityResult in Pixel XL?

I have implemented device camera functionality in my Android app. It's working well and good in all devices I've tested it out. Now I received a crash report from a user who is using Pixel XL.

Is there any change in Pixel XL for fetching the bitmap from onActivityResult?

My code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == AppConstants.ACTION_REQUEST_CAMERA && resultCode == Activity.RESULT_OK) {
        if(data != null) {
            if(data.getData()!=null) {
                try {
                    if (bitmap != null) {
                        bitmap.recycle();
                    }

                    InputStream stream = getContentResolver().openInputStream(data.getData());
                    bitmap = BitmapFactory.decodeStream(stream);
                    stream.close();
                    setCircularBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                bitmap = (Bitmap) data.getExtras().get("data");
                setCircularBitmap(bitmap);
            }
        } else {
            LogUtils.debug("path",initialURI.getEncodedPath());
            bitmap = getBitmapFromUri(initialURI);
            setCircularBitmap(bitmap);
            Toast.makeText(EditProfileActivity.this, "path " + initialURI.getEncodedPath(), Toast.LENGTH_SHORT).show();
        }
        isProfileImageUpdated = true;
        super.onActivityResult(requestCode, resultCode, data);
    } else if (requestCode == AppConstants.ACTION_REQUEST_GALLERY && resultCode == Activity.RESULT_OK) {
        if(data != null) {
            Uri selectedImage = data.getData();
            bitmap = getBitmapFromUri(selectedImage);
            setCircularBitmap(bitmap);
        } else {
            LogUtils.debug("path", "Gallery Data null");
        }
        isProfileImageUpdated = true;
        super.onActivityResult(requestCode, resultCode, data);
    }
}

The first condition is for device camera, and the second is for Gallery. And I have implemented runtime permissions too.

Got crash in this line:

bitmap = (Bitmap) data.getExtras().get("data");

Anything I'm missing out for Pixel XL? Didn't find any post pointing out to Pixel XL.

EDIT: Crash Report:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1025, result=-1, data=Intent {  }} to activity {com.paramsolutions.leadshare/com.paramsolutions.leadshare.activity.EditProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.BaseBundle.get(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.BaseBundle.get(java.lang.String)' on a null object reference
at com.paramsolutions.leadshare.activity.EditProfileActivity.onActivityResult(EditProfileActivity.java:323)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)

The line number points to the above-mentioned line, and I've Moto G4 Plus, which is also 7.0. I don't have that issue. Anything different in Pixel XL?

Does the app crashes for all the Android 7.0 devices.

If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.

Steps to replace file:// uri with content:// uri:

Add this to your manifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
    ...
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
    </application>
    </manifest>

then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

The final step is to change the line of code below in

Uri photoURI = Uri.fromFile(createImageFile());

to

Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

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