如何 select 图库中的文件?

How to select a file from the gallery?

我想通过 Android 画廊 select 一个 picture/movie 文件。我在 SO 中关注了一个类似的问题:Get/pick an image from Android's built-in Gallery app programmatically 这以某种方式解决了问题。

问题是,在这段代码中,returned 对象是 Bitmap 类型。我们如何 return 一个 raw 文件?我的意思是,我不希望它被解码为 Bitmap,我只想接收文件本身,原样 .

以下代码是本题部分解法:Get/pick an image from Android's built-in Gallery app programmatically

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();    

Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
/* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */

那么,我该怎么做呢?

你需要做的就是,

File myRawFile = new File(filePath);

然后使用文件得到你想要的。您可以从这里阅读更多关于文件 class 的信息,

https://developer.android.com/reference/java/io/File.html