0
我想在android中實現文件選擇器。我已經實現了下面的代碼來打開文件選擇器。如何從給定的uri獲得真正的路徑在android filechooser dropbox和谷歌驅動器?
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如下所示。
現在,當我點擊那個「文檔」我從那裏我可以選擇相同的文件像下面不同的應用程序。
在這裏你可以看到像谷歌驅動器和Dropbox的應用。現在我想從這個應用程序中選擇文件。 現在,當我從保管箱中選擇文件,然後在onActivityResult(int requestCode, int resultCode, Intent data)
我得到內容://com.dropbox.android.FileCache/filecache/2642b6eb-f9da-4775-b6c5-6cb4d6884018 uri從意圖。現在如何從這個uri獲得真正的路徑? 我想在我的活動中附加此文件。此外,我無法獲得谷歌驅動器文件的真實路徑。
不使用文件路徑,用'InputStream'代替 - 見'更多信息 – pskink
@pskink ContentResolver'文檔,所以你使用的InputStream我的意思將該文件寫在我的SD卡上,然後使用該SD卡的文件路徑。 –
爲什麼你需要文件路徑?你不能只使用'InputStream'嗎? (或'ParcelFileDescriptor' /'FileDescriptor')? – pskink