Issue
I can pick a file that is PDF or image by the following code:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"image/*", "application/pdf"
};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Select a file"),REQUEST_GET_SINGLE_FILE);
I receive the result in the following method:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_GET_SINGLE_FILE ) {
Uri selectedImageUri = data.getData();
....
....
I need to find that the selected file is a PDF or an image? How can I find the type of file?
Solution
I recommend you to have a read to ContentResolver documentation
and then read this Retriefe-info documentation
then you'll be able to get the extension of your file.
MIME type
Uri selectedImageUri = data.getData();
String mimeType = getContentResolver().getType(selectedImageUri);
It will return something like this :
"image/jpeg"
"image/png"
If you want to use a Cursor
you can do it as :
Cursor cursor = getContentResolver().query(selectedImageUri, null, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(0);
String filePath = cursor.getString(columnIndex);
String extension = filePath.substring(filePath.lastIndexOf(".") + 1); //will return pdf
}
cursor.close();
Answered By - Skizo-ozᴉʞS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.