嗨有誰知道誰顯示文件中的文件?我目前正在使用DownloadManager下載一個文件,併發射意圖在其他應用程序中打開該文件。但我正在尋找一種方法來查看我的應用程序內的文件,而不是將其發送到另一個?顯示來自文件的文件android
繼承人什麼,我目前正在做
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));
不同的文檔類型需要不同的觀看者代碼(例如文本文件與PDF與電子表格)。你試圖實施什麼,或者至少是你希望展示的文檔的範圍。 –
您可以添加一些與您在活動中打開的操作相匹配的操作,並且在過濾操作中將選擇您的活動以使用它。 –
它的一系列文檔類型,以便doc dock pdf xl xls –