我一直在开发一个从谷歌驱动器下载pdf的应用程序,并在Pdf查看器中打开它。一旦文件下载,它将在pdf查看器中打开文件(如果它存在于Android手机中)。您可以通过单击同一链接再次打开该文件。
但有时会发生的事情是文件下载因慢网或其他问题而中断。因此,当用户打开该文件时,它会显示"损坏的文件"。
那么如何删除该文件并通过应用程序检查它是否已损坏。
请回复.. 在此先感谢
公共类PDFTools {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
// long id = dm.enqueue(r); // dm.remove(id);
} 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;
}
}