有意图传递文件,我该如何检索它

Too*_*ink 11 java android android-intent

这是我要经过的.pictureFile是一个File

Intent intent = new Intent (context, ShowPicActivity.class);
intent.putExtra("picture", pictureFile);
Run Code Online (Sandbox Code Playgroud)

在接下来的活动中,我使用其中一个吸气剂来获取它?

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

Gui*_*ian 12

文件实现了serializable(首先检查通过intent发送对象.)(来源)

所以你可以这样做,只需将生成的对象转换为File,如下所示:

File pictureFile = (File)getIntent.getExtras().get("picture");
Run Code Online (Sandbox Code Playgroud)

应该没问题.(它使用'对象'的getter,它需要一个可序列化的对象并返回它.演员阵容应该足够了.)

  • `getSerializableExtra()` 不是更合适吗? (2认同)

Obl*_*obl 8

请尝试以下方法:

YourPictureClass picture = (YourPictureClass)getIntent.getExtras().get("picture");
Run Code Online (Sandbox Code Playgroud)

通过打电话给getExtras()你,Intent你得到一个实例Bundle.
通过get()课堂上的常规,Bundle您可以阅读每个Object传递给主叫的内容Intent.你唯一要做的就是将它解析回class你的对象是一个实例.


ban*_*ing 8

您可以这样发送:

intent.putExtra("MY_FILE", myFile);

并像这样检索它:

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

但是,将文件作为字符串引用发送可能会更好(任何人?),在这种情况下,您可以发送为:

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

并像这样检索/重建:

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