Open file always opens with PDF

0

Hi guys and girls (of course)

I have this method to open a file

public static void openFile(String filePath, Activity activity) {
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("file://"+filePath));
        activity.startActivityForResult(intent, 10);
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(context, "Error abriendo."+filePath+" Abrir manualmente", Toast.LENGTH_LONG).show();
    }
}

but when I set the path to my file for example image.jpg that is on my sdcard, it always tries to open it with the PDF-Drive, and of course this gives an error when trying to open this file. But when I open this image from the folder browser, it opens with the default image viewer. What am I doing wrong?

    
asked by San Juan 27.06.2017 в 23:39
source

2 answers

0

I tried to compile a lib.jar in java (NetBeans) but when I imported it into the android project it said that I could not find the Path class and the rest that gave me error

I solved creating my own getMimeType

In link I found a fairly complete list

I did a

HashMap <String, String> ~ <ext, mime>

and to get the mime to the hash that contains all I ask for the value of the extension

map.get(".bmp");
    
answered by 03.07.2017 / 23:50
source
0

Try adding an intent.setType ("image / jpeg") to set the type of file you wish to open. I leave a link to the documentation with several examples to send files and data between apps.

public static void openFile(String filePath, Activity activity) {
    try {
        Path path = new Paths.get(filePath);
        String mimeType = Files.probeContentType(path);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("file://"+filePath));
        intent.setType(mimeType);
        activity.startActivityForResult(intent, 10);
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(context, "Error abriendo."+filePath+" Abrir manualmente", Toast.LENGTH_LONG).show();
    }
}

link

link

    
answered by 28.06.2017 в 01:29