문제 설명
Android 암시적 의도 유효성 검사 (Android implicit intents validation)
내 애플리케이션에서 데이터를 Excel 파일에 저장하고 사용자에게 다음을 제안합니다.
- 장치에서 저장된 Excel 파일 열기;
- 파일 관리자에 파일 표시(폴더 포함).
- 이메일로 첨부된 파일 보내기(실제로는 이메일 클라이언트를 열고 파일을 첨부).
내 기기에서는 모든 것이 제대로 작동하는 것 같지만 질문은 다음과 같습니다.
- 이 암시적 의도를 확인하는 방법은 무엇입니까?
- 사용할 수 있는 Excel 뷰어/파일 관리자/이메일 클라이언트가 없다면?
- 어떻게 예측해야 합니까?
- 다른 유효성 검사를 추가해야 합니까?
코드
/**
* Opening saved file.
*
* @param file
* ‑ File to be opened
*/
private void openSavedFile(File file) {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
this.startActivity(intent);
}
/**
* Open a file browser and shows the folder which contains the file passed
* as a parameter.
*
* @param file
* ‑ File to be shown in file browser
*/
public void openFolder(File file) {
if (file.exists()) {
file = file.getParentFile();
Uri selectedUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
startActivity(intent);
}
}
/**
* Sends a file by email in attachment
*
* @param file
* ‑ file to be sent
*/
private void sendReportByMail(File file) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
// setting the type
emailIntent.setData(Uri.parse("mailto:"));
// the attaching the file
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, createEmailSubject());
// starting email intent with chooser
startActivity(Intent.createChooser(emailIntent, this.getResources()
.getString(R.string.report_email_chooser)));
}
참조 솔루션
방법 1:
Check that the implicit intent you fire ,any activity is their to handle that fire intent
PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
//Gotactivity to handle intent
startActivity(intent);
} else {
Log.d(TAG, "No Intent available to handle action");
}
(by user4702646、santoXme)