如何使用已安装的应用程序打开文件?
How to open a file using installed apps?
我想使用 Android 中安装的应用程序打开给定文件。我怎样才能做到这一点?
我正在使用 Google 中的以下代码,但它总是 returns "No app to open this file"
。但是,如果我在其他文件管理器中打开该文件,我会看到打开该文件的不同选项。代码:
public void open(View v) {
String name = ((TextView) v).getText().toString();
File f = new File(path + name);
if (f.isDirectory()) {
showFiles(f);
} else if (f.isFile()) {
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setData(Uri.fromFile(f));
if (openFileIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openFileIntent);
} else {
Toast.makeText(this, "No app to open this file.",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Selected item is not a file.",
Toast.LENGTH_SHORT).show();
}
}
您应该指定 MIME 类型,以便 Android 知道哪个应用程序能够打开/查看文件。这应该适合你:
public void open(View v) {
String name = ((TextView) v).getText().toString();
File f = new File(path + name);
if (f.isDirectory()) {
showFiles(f);
} else if (f.isFile()) {
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setDataAndType(Uri.fromFile(f), getMimeTypeFromFile(f));
if (openFileIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openFileIntent);
} else {
Toast.makeText(this, "No app to open this file.",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Selected item is not a file.",
Toast.LENGTH_SHORT).show();
}
}
private String getMimeTypeFromFile(File f){
Uri fileUri = Uri.fromFile(f);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(fileUri.toString());
String fileType
= MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
if (fileType == null)
fileType = "*/*";
return fileType;
}
我想使用 Android 中安装的应用程序打开给定文件。我怎样才能做到这一点?
我正在使用 Google 中的以下代码,但它总是 returns "No app to open this file"
。但是,如果我在其他文件管理器中打开该文件,我会看到打开该文件的不同选项。代码:
public void open(View v) {
String name = ((TextView) v).getText().toString();
File f = new File(path + name);
if (f.isDirectory()) {
showFiles(f);
} else if (f.isFile()) {
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setData(Uri.fromFile(f));
if (openFileIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openFileIntent);
} else {
Toast.makeText(this, "No app to open this file.",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Selected item is not a file.",
Toast.LENGTH_SHORT).show();
}
}
您应该指定 MIME 类型,以便 Android 知道哪个应用程序能够打开/查看文件。这应该适合你:
public void open(View v) {
String name = ((TextView) v).getText().toString();
File f = new File(path + name);
if (f.isDirectory()) {
showFiles(f);
} else if (f.isFile()) {
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setDataAndType(Uri.fromFile(f), getMimeTypeFromFile(f));
if (openFileIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openFileIntent);
} else {
Toast.makeText(this, "No app to open this file.",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Selected item is not a file.",
Toast.LENGTH_SHORT).show();
}
}
private String getMimeTypeFromFile(File f){
Uri fileUri = Uri.fromFile(f);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(fileUri.toString());
String fileType
= MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
if (fileType == null)
fileType = "*/*";
return fileType;
}