How to pick a file from gallery? - android

I want to pick a file (other than image, video or audio), like pdf, ppt, docx, txt etc.,,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),Constant.SELECT_FILE);
Above method is not working.
When I do the action Dialog box appears With message "No Application can perform this action"

Because there is no any application installed on device which is handle your intent with action Intent.ACTION_GET_CONTENT. So if your device running on Android 4.4 then try with Intent.ACTION_OPEN_DOCUMENT or Implement your own File Browser Activity.
Look at https://github.com/vaal12/AndroidFileBrowser and aFileChooser

Try this:
The problem is that there is no app installed to handle opening the PDF. You should use the Intent Chooser, like so:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}

Related

How to show docx and pdf files?

What do I want to achieve? I want to choose pdf or doc file and show them on recyclerview. What have I done?
Firstly, I tried to start activity like below:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Open document"), DOCUMENT_REQUEST_CODE);
When this attached document is clicked, I do following:
Intent docIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.providers.downloads.documents/document/doc_name"));
docIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(docIntent);
Above method opens Google Docs to load document and it can open only docs located on Google Drive. There is black view when I try to open document located in device storage. URI of document located in device is : content://com.android.providers.downloads.documents/document/doc_name and URI of document located in Google Drive is like : content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D430
After some searching I changed click method to following:
File file = new File("content://com.android.providers.downloads.documents/document/doc_name"); // url here is string type
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf"); //only pdf here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
But in above method, exists() returns false. Note, that all url that I pass are string type. So, how can I solve my problem?

Open the chooser for PDF before the actual PDF opens

currently, I'm trying to open my pdf files with a chooser. The problem I'm currently having is that the file tries to open before the chooser is visible. Only when I go back from the opened file I can see the chooser and choose my favorite application. I tried to modify my code according to several StackOverflow suggestions on how to use the chooser but even tho I tried them they won't work.
Here is my code to open the pdf:
case "PDF":
Intent pdfIntent = new Intent();
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
pdfIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri contentPDFUri = FileProvider.getUriForFile(context,
"com.ndlp.socialstudy.provider",
my_clicked_file);
pdfIntent.setDataAndType(contentPDFUri,"application/pdf");
Intent intentpdfChooser = pdfIntent.createChooser(pdfIntent, "Open With");
try {
context.startActivity(intentpdfChooser);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Please install a PDF app to view your file!", Toast.LENGTH_LONG).show();
// Instruct the user to install a PDF reader here, or something
}
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(file));
Intent j = Intent.createChooser(myIntent, "Choose an application to open with:");
startActivity(j);
Instead of calling
Intent intentpdfChooser = pdfIntent.createChooser(pdfIntent, "Open With");
try
Intent intentpdfChooser = Intent.createChooser(pdfIntent, "Open With");
and let the Android system figure out what are the applicaitons which can handle your pdfIntent
In one of my apps I'm using:
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri, "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open PDF using");
try {
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
Toast.makeText(mContext, "No Applications found to open pdf", Toast.LENGTH_SHORT).show();
}
and it's working for me.

How to open certain path in the Samsung Android My Files Programmatically

I'm using the following code to allow the user to access My Files from a Samsung android device. I'm trying to open certain path in the My Files when the user press the button. Right now it will open the default path. Can you tell me if it is possible to pass the path through when open My Files from Android? Below is the code that I use. Can you give me the code that will open My Files into certain Path? Thank you in advance for your help.
public void AccessingFiles(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", "*/*");
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
}
else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
For anyone looking for the solution to the same question this is how I open My Files pointed at a certain path:
File folder = new File(""); // construct the path
Intent fmIntent = new Intent(Intent.ACTION_MAIN);
fmIntent.setComponent(new ComponentName("com.sec.android.app.myfiles", "com.sec.android.app.myfiles.MainActivity"));
fmIntent.setData(Uri.parse(folder.toString()));
To be noted that fmIntent.setData(Uri.fromFile(folder)) did not work for me.
The solution is tested with My Files 1.19.0 and this is how I came to it:
I created a custom Launcher;
My Files can create home screen shortcuts to a specific path;
When the install shortcut intent was sent to my launcher I dumped its contents so that I can use them to create my fmIntent from the above code snippet.

Pick any kind of file via an Intent in Android

I would like to start an intentchooser for apps which can return any kind of file
Currently I use (which I copied from the Android email source code for file attachment)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app.
If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.
Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.
Not for camera but for other files..
In my device I have ES File Explorer installed and This simply thing works in my case..
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA),
but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE
Anyway here is my solution which works on Samsung and other devices:
public void openFile(String mimeType) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", mimeType);
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
This solution works well for me, and maybe will be useful for someone else.
this work for me on galaxy note
its show
contacts,
file managers installed on device,
gallery,
music player
private void openFile(Int CODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, CODE);
}
here get path in onActivityResult of activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
// do somthing...
super.onActivityResult(requestCode, resultCode, data);
}
This gives me the best result:
Intent intent;
if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
} else {
String[] mimeTypes =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"text/plain",
"application/pdf",
"application/zip", "application/vnd.android.package-archive"};
intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
}
Turns out the Samsung file explorer uses a custom action. This is why I could see the Samsung file explorer when looking for a file from the samsung apps, but not from mine.
The action is "com.sec.android.app.myfiles.PICK_DATA"
I created a custom Activity Picker which displays activities filtering both intents.
If you want to know this, it exists an open source library called aFileDialog that it is an small and easy to use which provides a file picker.
The difference with another file chooser's libraries for Android is that aFileDialog gives you the option to open the file chooser as a Dialog and as an Activity.
It also lets you to select folders, create files, filter files using regular expressions and show confirmation dialogs.
The other answers are not incorrect. However, now there are more options for opening files. For example, if you want the app to have long term, permanent acess to a file, you can use ACTION_OPEN_DOCUMENT instead. Refer to the official documentation: Open files using storage access framework. Also refer to this answer.

Opening pdf file programmatically is going into menu page?

How come every time i try to open a pdf file in my SDCARD using the following code, it doesn't actually open the pdf file itself, but goes into the menu of adobe reader? Is there anything wrong with my code?
Intent intent = new Intent();
File pdfFile = new File("/sdcard/sample.pdf");
Uri path = Uri.fromFile(pdfFile);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(path);
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);
No, there's nothing wrong. You've set the type to pdf and specified the package as adobe.reader. What that does is fire off an intent to launch the pdf in adobe reader. There's no way to display a pdf directly in your app without using a library (or writing code yourself) to render PDFs and display them.
Note that if you only set the type and not the package, the system will find whatever app is available to display PDFs
EDIT
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File( filename );
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
Here I am giving my pdf file name. You give your file name.
private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";
File file = new File(FILE);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}

Categories

Resources