Java 使用下载管理器android下载文件。如何检查文件是否完全下载
我一直在开发一个从谷歌硬盘下载pdf并在pdf查看器中打开的应用程序。一旦下载了文件,它就会在pdf查看器中打开文件(如果android手机中有)。您可以通过单击同一链接再次打开该文件 但有时会发生的情况是,由于网络速度慢或其他一些问题,文件下载在这段时间被中断。因此,当用户打开该文件时,会显示“已损坏的文件” 因此,如何删除该文件,并通过应用程序检查该文件是否已损坏 请回复。。 提前谢谢 公共类PDFTools{Java 使用下载管理器android下载文件。如何检查文件是否完全下载,java,android,Java,Android,我一直在开发一个从谷歌硬盘下载pdf并在pdf查看器中打开的应用程序。一旦下载了文件,它就会在pdf查看器中打开文件(如果android手机中有)。您可以通过单击同一链接再次打开该文件 但有时会发生的情况是,由于网络速度慢或其他一些问题,文件下载在这段时间被中断。因此,当用户打开该文件时,会显示“已损坏的文件” 因此,如何删除该文件,并通过应用程序检查该文件是否已损坏 请回复。。 提前谢谢 公共类PDFTools{ private static String file; private sta
private static String file;
private static final String PDF_MIME_TYPE = "application/pdf";
public static ProgressDialog progress = null;
public static boolean isNetworkAvailable(final Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
/**
* If a PDF reader is installed, download the PDF file and open it in a reader.
* Otherwise ask the user if he/she wants to view it in the Google Drive online PDF reader.<br />
* <br />
* <b>BEWARE:</b> This method
*
* @param context
* @param pdfUrl
* @return
*/
public static void showPDFUrl(final Context context, final String pdfUrl, final String name) {
if (isPDFSupported(context)) {
downloadAndOpenPDF(context, pdfUrl, name);
} else {
Toast.makeText(context, "PDF NOT SUPPORTED", Toast.LENGTH_SHORT).show();
}
if (pdfUrl == null) {
Toast.makeText(context, "NOT AVAILABLE", Toast.LENGTH_SHORT).show();
}
}
/**
* Downloads a PDF with the Android DownloadManager and opens it with an installed PDF reader app.
*
* @param context
* @param pdfUrl
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void downloadAndOpenPDF(final Context context, final String pdfUrl, final String name) {
// Get filename
//final String filename = pdfUrl.substring( pdfUrl.lastIndexOf( "/" ) + 1 );
// The place where the downloaded PDF file will be put
final File tempFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), name);
if (tempFile.exists()) {
try {
boolean bool = tempFile.canRead();
// If we have downloaded the file before, just go ahead and show it.
if (bool) {
openPDF(context, Uri.fromFile(tempFile));
return;
}else{
tempFile.delete();
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
// Show progress dialog while downloading
if (PDFTools.isNetworkAvailable(context)) {
progress = ProgressDialog.show(context, context.getString(R.string.pdf_show_local_progress_title), context.getString(R.string.pdf_show_local_progress_content), true);
progress.setCanceledOnTouchOutside(true);
// Create the download request
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(pdfUrl));
r.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, name);
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
r.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
r.setVisibleInDownloadsUi(true);
r.allowScanningByMediaScanner();
final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!progress.isShowing()) {
return;
}
context.unregisterReceiver(this);
progress.dismiss();
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Cursor cursor = dm.query(new DownloadManager.Query().setFilterById(downloadId));
Log.d("filepath", Environment.getExternalStorageState());
if (cursor.moveToFirst()) {
Log.d("In move to first", "Inside first if");
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
if (cursor.moveToFirst()) {
if (cursor.getCount() > 0) {
if (status == DownloadManager.STATUS_SUCCESSFUL) {
file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
// So something here on success
} else {
int message = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
File file1 = new File(file);
file1.delete();
}
}
}
}
cursor.close();
}
};
context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
dm.enqueue(r);
// Enqueue the request
}如果(status==DownloadManager.status\u SUCCESSFUL)不起作用,是否存在
的原因?当文件完全下载时,它就起作用了。当出现网络问题时,下载会在中间停止,控件不会进入if(状态==DownloadManager.status\u SUCCESSFUL)。当下载文件时出现网络问题时,下载会在两者之间停止,然后单击同一链接会出现文件损坏错误
} else {
Toast.makeText(context, "Check Your Network Connectivity",
Toast.LENGTH_LONG).show();
}
}
/**
* Show a dialog asking the user if he wants to open the PDF through Google Drive
* @param context
* @param pdfUrl
*/
/**
* Open a local PDF file with an installed reader
*
* @param context
* @param localUri
*/
public static final void openPDF(Context context, Uri localUri) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(localUri, PDF_MIME_TYPE);
try {
context.startActivity(i);
} catch (Exception e) {
File file1 = new File(file);
file1.delete();
}
}
/**
* Checks if any apps are installed that supports reading of PDF files.
*
* @param context
* @return
*/
public static boolean isPDFSupported(Context context) {
Intent i = new Intent(Intent.ACTION_VIEW);
final File tempFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "test.pdf");
i.setDataAndType(Uri.fromFile(tempFile), PDF_MIME_TYPE);
return context.getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}