skip to Main Content

I have been stuck on a problem for almost 1 week, so I allow myself to call on you.

I would like from an application that I am currently developing, to be able to open a file of any type of extension from the internal storage of the phone.

Example in picture:

If I click on an .mp3 file from my application that I am developing, Android would have to recognize the type and automatically suggest applications on my phone that can play the file:

"Ouvrir avec" means Open With

enter image description here

The same goes for the other types.

PDF:

enter image description here

JPG:
enter image description here

Do you have a solution to answer my problem?

I tried many attempts, my last was this:

    String path = "/storage/emulated/0/Download/Cours1.pdf";
   
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("content://" + path), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    getApplicationContext().startActivity(intent);

But my file does not open:

enter image description here

2

Answers


  1. Try adding this :

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
    

    Your final code should look like this:

    private void openFile() {
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
      MimeTypeMap mime = MimeTypeMap.getSingleton();
      File newFile = new File("/storage/emulated/0/Download/Cours1.pdf");
     String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1);
        String type = mime.getMimeTypeFromExtension(ext);
        
        Intent inte = new Intent();
      inte.setAction(Intent.ACTION_VIEW);
      getIntent().setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "com.your.package.fileProvider", newFile);
       inte.setDataAndType(contentUri, type);
      startActivity(inte);
    }
    

    Edit 1:
    Here is what I use (it works fine with me):

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
    
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://"+path),mimeType);
    intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    startActivity(intent);
    

    path and mimeType are Strings

    Edit 2:
    replace "content://" with "file://"

    If it worked please accept the answer and upvote it so more people can find it.

    Login or Signup to reply.
  2. You can do something like this:

    First go to manifest file and write the following tag under the <application> tag

    <provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="${applicationId}.provider"
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/providerpaths" />
    </provider>
    

    Then create a xml folder in res and create a file named: providerpaths.xml and then copy paste the code:

    <?xml version="1.0" encoding="utf-8"?>
      <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
      name="external_files"
      path="." />
    </paths>
    

    In java

    private void openFile(Context context ,String filePath) {
      File file = new File(filePath);
      Uri uri =  FileProvider.getUriForFile(Objects.requireNonNull(context.getApplicationContext()),BuildConfig.APPLICATION_ID + ".provider",file);
       String mime = context.getContentResolver().getType(uri);
       Intent intent = new Intent();
       intent.setAction(Intent.ACTION_VIEW);
       intent.setDataAndType(uri, mime);
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(intent);
    }
    

    How to use

    String path = "/storage/emulated/0/Download/Cours1.pdf";
      try {
        openFile(MainActivity.this ,path) ;
      }catch (Exception e){
        Toast.makeText(this, "Cannot open the file", Toast.LENGTH_SHORT).show();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search