如何在 android 中保存 onActivityResult Intent 数据?

How to save onActivityResult Intent data in android?

我正在使用 MediaProjectionManager 从应用程序录制视频,并授予保存音频和媒体存储的权限。

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

}

我想保存此 Intent 数据以供功能使用,这意味着我想在应用程序关闭和重新打开后重新使用此 Intent 数据,我还授予所有运行时权限。可以吗,请帮忙

谢谢。

您可以使用 SharedPreferences 将变量保存在 Android 中。

存储

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

取回

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

有关更多信息,请查看 here

如果您尝试了接受的答案但没有奏效,请让我通知您,从 android 文档 here 我们得知

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.

考虑到我在评论中提出的 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 问题,您还可以看到

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."