0
我想獲得一個文件的完整路徑,我拿起與Android瀏覽器。因此,我用下面的代碼:我無法獲得實際的路徑安卓文件
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"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "You don't have a browser installed", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==FILE_SELECT_CODE && resultCode==Activity.RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
Log.d(TAG, "URI del archivo a enviar: " + uri.toString());
// Get the path
final String path=Environment.getExternalStorageDirectory().toString()+uri.getPath();
Log.d(TAG, "Path del archivo a enviar: " +path);
File archivoPruebaParaSaberSiExiste = new File(path);
if(archivoPruebaParaSaberSiExiste.exists()) {
Log.d(TAG,"File exists");
else{
Log.d(TAG,"File does not exist");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
但是,我不得到的文件路徑:
The file path which i get is:
/storage/emulated/0/data/external/images/media/1949
The actual path is:
/storage/emulated/0/DCIM/Camera/20165481_133918.jpg
我不知道發生了什麼。
非常感謝你,這是兒子樂於助人! –