I'm working on an Android application which should be able to open a selected file from a specific folder.
I already tried this, but after selecting which application I want to open it, I got this message:
Impossible loading
After trying a lot of thread 1 and thread 2, I use these lines of code to do it:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("/mnt/sdcard/xxx/xxx/Pictures/xxx.jpg"), "image/*");
myContext.startActivity(intent);
How can I figure this out?
Try the following code.
Use this code ,which helped me to open all types of files ...
Download source code from here (https://deepshikhapuri.wordpress.com/2017/04/24/open-pdf-file-from-sdcard-in-android-programmatically/)
activity_main.xml:
MainActivity.java:
activity_pdf.xml:
PdfActivity.java:
Thanks!
directly you can use this code it will open all type of files
Try the below code. I am using this code for opening a PDF file. You can use it for other files also.
If you want to open files, you can change the
setDataAndType(path, "application/pdf")
. If you want to open different files with the same intent, you can useIntent.createChooser(intent, "Open in...");
. For more information, look at How to make an intent with multiple actions.Try this link. It might help you.
The link contains:
Example to show how to create an image chooser.
The code from my sample project above shows how to create a dialog that shows two options to select the image source. The first is from Camera, where the picture is taken directly from camera, and the second is from saved images on sdcard.
line 14: Define two image source options, from camera and from SD card.
line 16-48: Set up the image picker dialog.
line 22: If the user chooses to take a picture from the camera, create an intent to open the camera app with MediaStore.ACTION_IMAGE_CAPTURE action.
line 23: Create a temporary file to hold the image from the camera.
line 24: Get the URI of the temporary file
line 39: If the user chooses to select an image from the SD card, start the intent to open the image chooser dialog. The image chooser dialog will display list File Manager (if it exists) apps and the default gallery app.
line 55: Show the image picker dialog.
line 61: Override the onActivityResult method to handle the selected image.
line 67: If the user selects an image from the SD card, get the URI of the selected image (line 68)
line 69: Assume the user selects the image from the SD card using the gallery app. The URI from the gallery app does not give the real path to the selected image, so it has to be resolved on the content provider. Method getRealPathFromURI used to resolve the real path from the URI.
line 71: If the path is null, assume the user selects the image using the File Manager app. The File Manager app returns different information than the gallery app. To get the real path to the selected image, use getImagePath method from the URI (line 72).
line 75: Get the bitmap.
line 77: If the user chooses to take a picture from the camera, get the real path of the temporary file that was previously defined on line 24-25.
line 78: Get the bitmap.
line 81: Display the bitmap on the image view.