4

My Codes are:

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); 

How to get the file path,name,extension? (suppose files are in doc,pdf,csv format)

1
  • 2
    have you got any answer Commented Aug 1, 2017 at 10:29

7 Answers 7

7

It return the file extention like pdf, doc .. etc

 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; } 

It returns you the real path where you get the file name. One friend use this way and it is really useful.

Get filename and path from URI from mediastore

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(); } } } 

You get file name from this

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; } 
Sign up to request clarification or add additional context in comments.

Comments

5

I am late but it can help others here is the solution.

 Uri uri = data.getData(); ContentResolver cr = this.getContentResolver(); String mime = cr.getType(uri); 

Comments

1

you can use like this

@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(); } } 

1 Comment

No i don't want image i want other file like pdf,doc what will be in place of "MediaStore.Images.Media.DATA" if i want pdf or doc file please help.
0

Once try as follows if the FileReturnedIntent contains File as extras

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); 

Hope this will helps you.

1 Comment

FileReturnedIntent.getExtras() is null
0

Nova day in Kotlin:

val myFile = File(data?.data!!.path) 

Comments

0

for any file name from any path you can use this method :

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(); } 

Comments

-5

you can put String Extra to intent using

intent.putExtra("path", your_path); 

and get it in onActivityResult by

FileReturnedIntent.getStringExtra("path"); 

2 Comments

How is he going to insert the path into the Intent if he is receiving the intent from the ACTION_GET_CONTENT request?
this answer does not help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.