安卓文件选择器 [英] Android file chooser
问题描述
我想做一个文件上传器.因此我需要一个文件选择器,但我不想自己写这个.我找到了 OI 文件管理器,我认为它适合我.但是如何强制用户安装 OI 文件管理器?如果我不能,是否有更好的方法在我的应用程序中包含文件管理器?谢谢
I want to make a file uploader. And I hence I need a file chooser but I don't want to write this by myself. I find OI file manager and I think it suits me. But how can I force user to install OI file manager? If I cannot , is there a better way to include a file manager in my app? Thx
推荐答案
编辑(2012 年 1 月 2 日):
我创建了一个小型开源 Android 库项目来简化这个过程,同时还提供了一个内置文件浏览器(以防用户没有).使用起来极其简单,只需要几行代码.
I created a small open source Android Library Project that streamlines this process, while also providing a built-in file explorer (in case the user does not have one present). It's extremely simple to use, requiring only a few lines of code.
您可以在 GitHub 上找到它:aFileChooser.
You can find it at GitHub: aFileChooser.
原创
如果您希望用户能够选择系统中的任何文件,您将需要包含您自己的文件管理器,或者建议用户下载一个.我相信你能做的最好的事情就是在 Intent.createChooser()
中寻找可打开"的内容,如下所示:
If you want the user to be able to choose any file in the system, you will need to include your own file manager, or advise the user to download one. I believe the best you can do is look for "openable" content in an Intent.createChooser()
like this:
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
然后您将在 onActivityResult()
中监听所选文件的 Uri
,如下所示:
You would then listen for the selected file's Uri
in onActivityResult()
like so:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "File Uri: " + uri.toString());
// Get the path
String path = FileUtils.getPath(this, uri);
Log.d(TAG, "File Path: " + path);
// Get the file instance
// File file = new File(path);
// Initiate the upload
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
我的 FileUtils.java
中的 getPath()
方法是:
The getPath()
method in my FileUtils.java
is:
public static String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
这篇关于安卓文件选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!