[英]Pick a file from an SD card using Intent
Does Android have a way to browse and pick any file from an SD card using intents? Android是否有办法使用意图浏览并从SD卡中选择任何文件?
Something like: 就像是:
String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));
I am trying to send a file to other devices using Bluetooth. 我正在尝试使用蓝牙将文件发送到其他设备。 I can send if I give the full path of the file name in my code. 如果我在代码中提供文件名的完整路径,我可以发送。 I want my users to pick the file that should be send. 我希望我的用户选择应该发送的文件。
You can use the following code: 您可以使用以下代码:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);
Then you can get the path in onActivityResult: 然后你可以在onActivityResult中获取路径:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESTCODE_PICK_VIDEO
&& resultCode == Activity.RESULT_OK) {
Uri videoUri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}
For general browsing use this (eg Music file): 对于一般浏览使用此(例如音乐文件):
Intent intent = new Intent();
intent.setType("*/*");
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent = Intent.createChooser(intent, "Select file");
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = { "audio/*", "video/*" };
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
}
startActivityForResult(intent, Constants.REQUEST_BROWSE);
And receive the browsed data here: 并在此处接收浏览的数据:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_BROWSE
&& resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
if (uri != null) {
// TODO: handle your case
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.