user3312537
user3312537

Reputation: 3

I want to download the file from server and view the file in android with various file Extensions like (pdf,doc,mp4,jpeg))

I want to download the file from server and view the file in android with various file Extensions like (pdf,doc,mp4,jpeg) i have try this

Intent intent = new Intent(Intent.ACTION_VIEW);
       File file1 = new File(Environment.getExternalStorageDirectory(),"report.pdf");

       intent.setDataAndType( Uri.fromFile( file1 ), "application/.*" );


     startActivity(intent);

but no success should i have to give the individual extension in this please suggest the way to solve this

Upvotes: 0

Views: 107

Answers (1)

Mourice
Mourice

Reputation: 597

See this code for your reference

filePath is a actual path of the file

                        Uri fileUri = Uri.fromFile(new File(filePath));

                        Uri new_uri = Uri.parse("file://"
                                + fileUri.getPath());

                        Intent intent = new Intent(Intent.ACTION_VIEW,
                                new_uri);

                        MimeTypeMap myMime = MimeTypeMap.getSingleton();
                        String fileExt = filePath.substring(filePath
                                .lastIndexOf(".") + 1);
                        String mimeType = myMime
                                .getMimeTypeFromExtension(fileExt);
                        intent.setDataAndType(new_uri, mimeType);

                        startActivity(intent);

Upvotes: 1

Related Questions