このコードを使用して、指定したアプリでファイルを開きます。
File file = new File("/path/to/file.ext");
if(file.exists())
{
Uri path=Uri.fromFile(file);
Intent intent=new Intent(Intent.ACTION_VIEW, path);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setPackage("com.example.myapp");
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(this, "No software found", Toast.LENGTH_SHORT).show();
}
.zip ファイルでは完全に機能しますが、型にはまらないファイル拡張子では機能せず、ActivityNotFoundException がスローされます。プログラムでファイルの種類をアプリに関連付ける方法、またはアプリに特定のファイルを開くように強制する方法はありますか?