您知道有谁知道从文件中显示文档的人吗?我目前正在使用DownloadManager下载文件并触发在其他应用程序中打开文件的意图。但我正在寻找一种方法来查看我的应用程序中的文档,而不是将其发送给另一个?
继承人我正在做的事情
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(filename);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
ContextWrapper c = new ContextWrapper(getBaseContext());
final String filePath = c.getFilesDir().getPath();
request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();
BroadcastReceiver onComplete = new BroadcastReceiver() {
private String fileExt(String url){
if (url.indexOf("?")>-1) {
url = url.substring(0,url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") );
if (ext.indexOf("%")>-1) {
ext = ext.substring(0,ext.indexOf("%"));
}
if (ext.indexOf("/")>-1) {
ext = ext.substring(0,ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
@Override
public void onReceive(Context context, Intent intent) {
String path = getFilesDir().getPath() +"/" + filename;
File f = new File(path);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
//Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(f.toString()).substring(1));
newIntent.setDataAndType(Uri.fromFile(f), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(context, "No handler for this type of file.", 4000).show();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
答案 0 :(得分:1)
每个常见的office-ish类型文档都需要自己的非平凡的查看器。有些可能有现成的开源实现,但可能没有提供良好用户体验的性能。
我仍然建议您使用意图直接观看,但您可能希望将用户吸引到QuickOffice ,如果他们没有现有的观看者,那么Google可以免费使用这些用户。
来自Android文档:
http://developer.android.com/guide/components/intents-filters.html
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}
答案 1 :(得分:0)
如果您想在应用程序中查看文件,那么您将需要从头开始实现自己的查看器或找到可以利用的开源实现。请记住,如果文档是任意类型,则您需要查看要显示的每种文件类型的查看器。 Here's a thread关于为PDF的
创建这样的查看器这可能对您没有用,但我还要提一下,您可以调用外部应用程序,使其看起来好像是应用程序的一部分,并且在打开的应用程序列表中没有自己的条目。
AFAIK这些是唯一可用的选项。