Android是否有办法使用意图浏览并从SD卡中选择任何文件?
类似的东西:
String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));
我正在尝试使用蓝牙将文件发送到其他设备。如果我在代码中提供文件名的完整路径,我可以发送。我希望我的用户选择应该发送的文件。
答案 0 :(得分:9)
您可以使用以下代码:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);
然后你可以在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);
}
}
答案 1 :(得分:5)
对于一般浏览,请使用此功能(例如音乐文件):
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);
并在此处接收浏览的数据:
@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
}
}
}