我想在android中实现文件选择器。我已经实现了以下代码来打开文件选择器。
public static void showFileChooser(Activity activity, Fragment fragment) {
try {
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "example");
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mCapturedImageURI = Uri.fromFile(file); // save to the private variable
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
captureIntent.putExtra("capturedimageuri", mCapturedImageURI.toString());
// Intent for Audio Recording
final Intent audioRecordIntent = new Intent();
audioRecordIntent.setAction(IAdoddleConstants.ACTION_AUDIO_RECORD);
final Intent videoRecordIntent = new Intent();
videoRecordIntent.setAction(IAdoddleConstants.ACTION_VIDEO_RECORD);
// Use the GET_CONTENT intent from the utility class
Intent target = com.asite.adoddle.filechooser.FileUtils.createGetContentIntent();
// Create the chooser Intent
if (activity != null) {
Intent intent = Intent.createChooser(
target, activity.getString(R.string.chooser_title));
intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent});
activity.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE);
} else {
Intent intent = Intent.createChooser(
target, fragment.getString(R.string.chooser_title));
intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent});
fragment.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE);
}
} catch (ActivityNotFoundException e) {
AdoddleLog.e(DEBUG_TAG, "Error:" + e);
} catch (Exception ex) {
ex.printStackTrace();
CommonUtilities.showToast(activity, activity.getString(R.string.error_message), Toast.LENGTH_LONG);
}
}
现在它打开filechooser,如下所示。
现在,当我点击“文档”时,我会从不同的应用程序中选择相同的文件,如下所示。
在这里,您可以看到Google云端硬盘和Dropbox等应用。现在我想从这个应用程序中选择文件。
现在,当我从dropbox中选择文件,然后在onActivityResult(int requestCode, int resultCode, Intent data)
中,我从意图中得到内容://com.dropbox.android.FileCache/filecache/2642b6eb-f9da-4775-b6c5-6cb4d6884018 uri。现在如何从这个uri获得真正的路径?
我想在我的活动中附上这个文件。此外,我无法获得谷歌驱动器文件的真实路径。
答案 0 :(得分:-1)
要使用图像或视频真实路径:
protected String getPhotoUriRealPath(Uri contentURI) {
String path = "";
if (contentURI == null) {
return path;
}
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
path = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path = cursor.getString(idx);
}
if (cursor != null) {
cursor.close();
}
return path;
}
protected String getVideoUriRealPath(Uri contentURI) {
String path = "";
if (contentURI == null) {
return path;
}
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
path = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
path = cursor.getString(idx);
}
if (cursor != null) {
cursor.close();
}
return path;
}