Nafeez
Nafeez

Reputation: 48

How to pick a file from gallery?

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"

Upvotes: 2

Views: 984

Answers (2)

user370305
user370305

Reputation: 109257

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

Upvotes: 2

Lokesh
Lokesh

Reputation: 5378

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
}   

Upvotes: 0

Related Questions