0

please see this code bellow as you can see my FilePath only gets this path /document/1393, so how do i get my absolute path like /sdcard/document/test.docx ?

 // Get Result Back
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case FILE_SELECT_CODE:
            if(resultCode==-1){
                String FilePath = data.getData().getPath();
                Log.d("onActivityResult", "FilePath = " + FilePath);
                cityEditText.setText(FilePath);
            }
            break;

    }

}
anuloo
  • 15
  • 10

1 Answers1

1

Try this:

Uri uri = Uri.fromFile(new File("/path/to/file"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Verify that there are applications registered to handle this intent
// (resolveActivity returns null if none are registered)
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

Depending on where you are executing this code from (in an activity, view, fragment) you may need to adjust getPackageManager() to getActivity().getPackageManager() or getContext().getPackageManager().

Abtin Gramian
  • 1,630
  • 14
  • 13
  • much appreciated your quick response. but i am a bit confused. this will defenatly works wuth any extention like .pdf or doc or .docx or png etc... you got the idea. – anuloo Oct 13 '16 at 23:55
  • Yes this should determine the appropriate action based on the uri. If you want to specify yourself you can use `intent.setDataAndType(uri, "application/pdf");` for example and adjust the second mime/file type argument as necessary. For word you would use `"application/msword"`, for powerpoint `"application/vnd.ms-powerpoint"`, for excel `"application/vnd.ms-excel"` etc. In this case you would create the intent object without the second uri argument. – Abtin Gramian Oct 14 '16 at 00:04
  • thanks i will try tomorrow and i will let you know how i get on. thanks in a million. – anuloo Oct 14 '16 at 00:52
  • what i mean is lets say i have a test.doc in my Downloads on my device how do i get that so i can change this Uri uri = Uri.fromFile(new File("/path/to/file")); – anuloo Oct 15 '16 at 23:26
  • The file should be part of your app data not in the downloads folder so you would know the filename and be able to construct the path with `getApplicationInfo().dataDir` or `getFilesDir().getAbsolutePath()` or `getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir`. If you want to test with a file in your downloads checkout [this](http://stackoverflow.com/questions/28262275/how-to-get-complete-path-and-name-of-each-file-in-download-directory-in-android). Take a step back and explain what you're trying to do because I may be misunderstanding. – Abtin Gramian Oct 16 '16 at 02:03
  • That was perfect thanks , but now i have a small issue, my when i browse for a document the path i get is /document/1393, so how do i get my absolute path like /sdcard/document/test.docx ? – anuloo Oct 16 '16 at 20:25
  • i posted the code in the question section to see what i am using – anuloo Oct 16 '16 at 20:32