1

I tried many ways to open file but I'm not able to open file from sd-card using intent.

From internal memory easily open file, but from sd-card file open is difficult. Also able to open file from web-url.

Permission :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

b = true -> file uri

b = false -> web url

url = /mnt/media_rw/9016-4EF8/123.pdf

Here is my code for open file from sd-card:

 public static void openFile(Activity context, String url, Boolean b) throws
 IOException 

{


    File file = new File(url);

    Uri uri=null;


    if (b)
        uri = Uri.fromFile(file);
    else
        uri = Uri.parse(url);


    Intent intent = new Intent(Intent.ACTION_VIEW);


    if (url.toString().toLowerCase().contains(".doc") || url.toString().toLowerCase().contains(".docx")) {
        // Word document
        intent.setDataAndType(uri, "application/msword");
    } else if (url.toString().toLowerCase().contains(".pdf")) {
        // PDF file
        intent.setDataAndType(uri, "application/pdf");
    } else if (url.toString().toLowerCase().contains(".ppt") || url.toString().toLowerCase().contains(".pptx")) {
        // Powerpoint file
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if (url.toString().toLowerCase().contains(".xls") || url.toString().toLowerCase().contains(".xlsx")) {
        // Excel file
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if (url.toString().toLowerCase().contains(".zip") || url.toString().toLowerCase().contains(".rar")) {
        // WAV audio file
        intent.setDataAndType(uri, "application/x-wav");
    } else if (url.toString().toLowerCase().contains(".rtf")) {
        // RTF file
        intent.setDataAndType(uri, "application/rtf");
    } else if (url.toString().toLowerCase().contains(".wav") || url.toString().toLowerCase().contains(".mp3")) {
        // WAV audio file
        intent.setDataAndType(uri, "audio/x-wav");
    } else if (url.toString().toLowerCase().contains(".gif")) {
        // GIF file
        intent.setDataAndType(uri, "image/gif");
    } else if (url.toString().toLowerCase().toLowerCase().contains(".jpg") || url.toString().toLowerCase().contains(".jpeg") || url.toString().toLowerCase().contains(".png")) {
        //JPG file
        intent.setDataAndType(uri, "image/*");
    } else if (url.toString().toLowerCase().contains(".txt")) {
        // Text file
        intent.setDataAndType(uri, "text/plain");
    } else if (url.toString().toLowerCase().contains(".3gp") || url.toString().toLowerCase().contains(".mpg") || url.toString().toLowerCase().contains(".mpeg") || url.toString().toLowerCase().contains(".mpe") || url.toString().toLowerCase().contains(".mp4") || url.toString().toLowerCase().contains(".avi")) {
        // Video files
        intent.setDataAndType(uri, "video/*");
    } else {
        intent.setDataAndType(uri, "*/*");
    }
    //intent.setDataAndType(uri, "application/*");


    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
        Log.e("Error -> ", e + "");
    }
}

Output:

File not open using intent

4 Answers4

0
Try using this, 

 File dir    =   new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/PersonData");
 File file   =   new File(dir, "Data.pdf");

Here "PersonData" is the folder inside sdcard, and the filename is Data.pdf

Ranjan
  • 1,326
  • 18
  • 38
  • File not found (/storage/emulated/0/PersonData/Data.pdf : open failed : ENOENT (No such file or directory)) – Trupen Meruliya Dec 27 '16 at 11:55
  • i got path = /mnt/media_rw/9016-4EF8/PersonData/Data.pdf from memory card – Trupen Meruliya Dec 27 '16 at 11:56
  • in my project first i attach any type of document (like doc,pdf,mp3,mp4 etc..) after attach when i click on attachment then open this document with releted default intent for internal memory attachment run complete but from memory card attachment not able to open – Trupen Meruliya Dec 27 '16 at 12:00
  • Is you are running it in an emulator, try it in a real device. Make sure you have PersonData named folder and and Data.pdf inside it. – Ranjan Dec 27 '16 at 12:15
  • NO , I run in my fone – Trupen Meruliya Dec 27 '16 at 12:16
  • I am using the same thing for fetching csv in my app, and it is working fine – Ranjan Dec 27 '16 at 12:16
  • ok sir thanks.. But in getExternalStorageDirectory() i got internal memory path – Trupen Meruliya Dec 27 '16 at 12:20
  • content://com.android.externalstorage.documents/document/9016-4EF8%3AProgram.docx This uri i actual got when i attach file – Trupen Meruliya Dec 27 '16 at 12:25
  • Try this link it will guide you more, As you re not fimiliar with File input output options. http://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory – Ranjan Dec 27 '16 at 12:29
0

It seems that you should pass your url in Intent Like so:

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Hetfieldan24
  • 198
  • 11
  • File not found (/mnt/media_rw/9016-4EF8/123.pdf) open failed EACCESS (permission denied) Here i give permission as well as file at /mnt/media_rw/9016-4EF8/123.pdf – Trupen Meruliya Dec 27 '16 at 12:04
0

Be sure <uses-permission 's place. It should be like below;

 <manifest>
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <application>
             <activity> 
              ...
             </activity>
      </application>
  </manifest> 
oyenigun
  • 587
  • 6
  • 15
  • What is your test device android version? if >=M, permission management like this : https://developer.android.com/training/permissions/requesting.html – oyenigun Dec 27 '16 at 13:03
0

You do not have direct filesystem access to arbitrary locations on removable storage, starting with Android 4.4.

Since nothing on Android 4.4+ should be giving you that filesystem path, this normally will not be a problem. It will be a problem if you are hard-coding that path, since you do not have access to that location.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491