[英]How to pass a URI to an intent?
I'm trying to pass a URI-Object to my Intent in order to use that URI in another activity.我正在尝试将 URI 对象传递给我的 Intent,以便在另一个活动中使用该 URI。
How do I pass a URI?如何传递 URI?
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri);
startActivity(intent);
this.finish();
How do I use now this URI in my other activity?我现在如何在其他活动中使用此 URI?
imageUri = extras.getString("imageUri"); // I know thats wrong ...
you can store the uri as string您可以将 uri 存储为字符串
intent.putExtra("imageUri", imageUri.toString());
and then just convert the string back to uri like this然后像这样将字符串转换回 uri
Uri myUri = Uri.parse(extras.getString("imageUri"));
The Uri
class implements Parcelable
, so you can add and extract it directly from the Intent
Uri
类实现Parcelable
,因此您可以直接从Intent
添加和提取它
// Add a Uri instance to an Intent
intent.putExtra("imageUri", uri);
// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("imageUri");
You can use the same method for any objects that implement Parcelable
, and you can implement Parcelable
on your own objects if required.您可以对任何实现
Parcelable
对象使用相同的方法,如果需要,您可以在自己的对象上实现Parcelable
。
In Intent, you can directly put Uri. Intent中可以直接放Uri。 You don't need to convert the Uri to string and convert back again to Uri.
您不需要将 Uri 转换为字符串并再次转换回 Uri。
Look at this simple approach.看看这个简单的方法。
// put uri to intent
intent.setData(imageUri);
And to get Uri back from intent:并从意图中恢复 Uri:
// Get Uri from Intent
Uri imageUri=getIntent().getData();
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
this.finish();
And then you can fetch it like this:然后你可以像这样获取它:
imageUri = Uri.parse(extras.getString("imageUri"));
here how I use it;在这里我如何使用它; This button inside my CameraActionActivity Activity class where I call camera
我的CameraActionActivity Activity 类中的这个按钮,我在其中调用了相机
btn_frag_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intenImatToSec = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intenImatToSec, REQUEST_CODE_VIDEO);
//intenImatToSec.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//intenImatToSec.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
//Toast.makeText(getActivity(), "Hello From Camera", Toast.LENGTH_SHORT).show();
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_IMG) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
Intent intentBitMap = new Intent(getActivity(), DisplayImage.class);
// aldıgımız imagi burda yonlendirdiğimiz sınıfa iletiyoruz
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intentBitMap.putExtra("byteArray", _bs.toByteArray());
startActivity(intentBitMap);
} else if (requestCode == REQUEST_CODE_VIDEO) {
Uri videoUrl = data.getData();
Intent intenToDisplayVideo = new Intent(getActivity(), DisplayVideo.class);
intenToDisplayVideo.putExtra("videoUri", videoUrl.toString());
startActivity(intenToDisplayVideo);
}
}
}
And my other DisplayVideo Activity Class和我的其他DisplayVideo活动类
VideoView videoView = (VideoView) findViewById(R.id.videoview_display_video_actvity);
Bundle extras = getIntent().getExtras();
Uri myUri= Uri.parse(extras.getString("videoUri"));
videoView.setVideoURI(myUri);
If you want to use standard extra data field, you would do something like this:如果你想使用标准的额外数据字段,你可以这样做:
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra(Intent.EXTRA_STREAM, imageUri.toString());
startActivity(intent);
this.finish();
The documentation for Intent says: Intent 的文档说:
EXTRA_STREAM added in API level 1
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent,
used with ACTION_SEND to supply the data being sent.
Constant Value: "android.intent.extra.STREAM"
You don't have to use the built-in standard names, but it's probably good practice and more reusable.您不必使用内置的标准名称,但这可能是一种很好的做法,而且更可重用。 Take a look at the developer documentation for a list of all the built-in standard extra data fields.
查看开发人员文档以获取所有内置标准额外数据字段的列表。
The Uri.parse(extras.getString("imageUri"))
was causing an error: Uri.parse(extras.getString("imageUri"))
导致错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
So I changed to the following:所以我改为以下内容:
intent.putExtra("imageUri", imageUri)
and和
Uri uri = (Uri) getIntent().get("imageUri");
This solved the problem.这解决了问题。
you can do like this.你可以这样做。 imageuri can be converted into string like this.
imageuri 可以像这样转换成字符串。
intent.putExtra("imageUri", imageUri.toString()); intent.putExtra("imageUri", imageUri.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.