How to save onActivityResult Intent data in android?

581 Views Asked by At

i am using MediaProjectionManager to record video from app,and also give permission for save audio and media storage.

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

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

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        Intent permissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
        startActivityForResult(permissionIntent, SCREEN_RECORD_REQUEST_CODE);
    }



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

}

i want to save this Intent data for feature use, means i want to reuse this intent data from after application close and re-open time, i give all runtime permission also. is it possible, then please help.

Thanks.

2

There are 2 best solutions below

6
On BEST ANSWER

You can use SharedPreferences to save Variables in Android.

To store

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
                SharedPreferences.Editor editor = settings.edit();
                String uriString = data.toUri(requestCode); 
                editor.putString("Contacts_app", uriString);
                editor.commit();

To retreive

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
    String contactsApp = settings.getString("Contacts_app", null);
    try {
        telApp = Intent.parseUri(contactsApp, 0);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

For further information look here.

0
On

If you´ve tried the accepted answer and has not worked let me inform you that from the android documentation here we learn that

Note: Parcel is not a general-purpose serialization mechanism, and you should never store any Parcel data on disk or send it over the network.

And taking into account that Intent implements Parcelable then no wonders why that does not work. I tried with Gson to serialize and then deserialize the intent but did not work, on this question I asked on the comments you can also see that

Beyond the problem that not everything in an Intent can be written successfully to disk, your permission to capture screenshots/screencasts lives only as long as your process does. Writing the Intent to disk will not change that."