我的代码是:
1.
File file = new File(Environment.getExternalStorageDirectory(),"myFolder");
Log.d("path", file.toString());
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivityForResult(intent,0);
2.
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent FileReturnedIntent) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, FileReturnedIntent);
如何获取文件路径、名称、扩展名? (假设文件是doc、pdf、csv格式)
它返回文件扩展名,如 pdf、doc .. 等
public static String getMimeType(Context context, Uri uri) {
String extension;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
它返回您获取文件名的真实路径。有朋友用过这个方法,真的很有用。
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
您可以从中获取文件名
public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}
我迟到了,但它可以帮助其他人,这就是解决方案。
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
String mime = cr.getType(uri);
你可以这样使用
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
}
如果 FileReturnedIntent 包含 File 作为额外内容,请尝试如下操作
Bundle data1=FileReturnedIntent.getExtras();
File f=(File)data1.get(key);
String path=f.getAbsolutePath();
System.out.println("The file path along with extension is : "+path);
希望这对您有帮助。
Kotlin 的 Nova 日:
val myFile = File(data?.data!!.path)
对于任何路径中的任何文件名,您可以使用此方法:
public static String getFileNameFromPath(String path) {
String reverse = new StringBuilder(path).reverse().toString();
StringBuilder name = new StringBuilder();
for (int i = 0; i < reverse.length(); i++) {
if (reverse.charAt(i) == '/') break;
name.append(reverse.charAt(i));
}
return name.reverse().toString();
}
你可以使用 String Extra 来达到目的
intent.putExtra("path", your_path);
并通过
在onActivityResult中获取它FileReturnedIntent.getStringExtra("path");