7

Android 是否可以使用 Intent 从 SD 卡中浏览和选择任何文件?

就像是:

String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));

我正在尝试使用蓝牙将文件发送到其他设备。如果我在我的代码中给出文件名的完整路径,我可以发送。我希望我的用户选择应该发送的文件。

4

2 回答 2

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);
    }
}
于 2014-04-02T09:30:57.243 回答
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
        }
    }
}
于 2015-04-30T07:30:32.100 回答