Для того, чтобы открыть файл, вы можете использовать следующий метод. Если приложение не может обрабатывать данный файл, оно просто показывает Toast
, что приложение не найдено.
private void viewFile(String filePath, String title, int fileType) {
Uri uri = Uri.parse("file://" + filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
String dataAndType = getIntentDataAndType(filePath);
intent.setDataAndType(uri, dataAndType);
intent.putExtra(Intent.EXTRA_TITLE, title);
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "No Application found", Toast.LENGTH_SHORT).show();
}
}
ОБНОВЛЕНО: Для нахождения типа пантомимы файла.
private String getIntentDataAndType(String filePath) {
String exten = "";
int i = filePath.lastIndexOf('.');
// If the index position is greater than zero then get the substring.
if (i > 0) {
exten = filePath.substring(i + 1);
}
String mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(exten);
mimeType = (mimeType == null) ? "*/*" : mimeType;
return mimeType;
}
Эй Kartheek, спасибо за ответ, это getIntentDataAndType является любой метод, который вы определили, потому что, когда я с помощью кода его о том, что мне нужно создать этот метод отдельно. – sid
Я удалил параметр fileType, теперь такой переменной нет. – Kartheek
yup просто посмотрите на свой код, кажется, что тип файла не использовался нигде в коде .. для этого глупого вопроса: D – sid