Passing File with intent, how do i retrieve it

2020-05-26 09:52发布

Here is what I am passing. pictureFile is a File

Intent intent = new Intent (context, ShowPicActivity.class);
intent.putExtra("picture", pictureFile);

In the next activity which one of the getters do I use to get it?

Intent intent = getIntent(); .....?

3条回答
姐就是有狂的资本
2楼-- · 2020-05-26 10:16

Try the following:

YourPictureClass picture = (YourPictureClass)getIntent.getExtras().get("picture");

By calling getExtras() in your Intent you get an instance of Bundle.
With the normal get() in class Bundle you can read every Object you pass to the calling Intent. The only thing you have to do is to parse it back to the class your object is an instance of.

查看更多
家丑人穷心不美
3楼-- · 2020-05-26 10:21

File implements serializable ( first thing to check to send an object through an intent. ) ( source )

so you can do it, just cast the resulting object to File like this :

File pictureFile = (File)getIntent.getExtras().get("picture");

It should be fine. (it use the getter for 'object' which needs a serializable object and return it. The cast should be enough.)

查看更多
相关推荐>>
4楼-- · 2020-05-26 10:32

You can send it like this:

intent.putExtra("MY_FILE", myFile);

and retrieve it like this:

File myFile = (File)intent.getSerializableExtra("MY_FILE");

However, it may be better (anyone?) to send the file as a string reference, in which case you could send as:

intent.putExtra("MY_FILE_STRING", myFile.toString());

and retrieve/reconstruct like this:

File myFile = new File(intent.getStringExtra("MY_FILE_STRING"));

查看更多
登录 后发表回答