我正在从服务器下载pdf文件。最近我遇到了
在下载过程中断开互联网连接时,我
无法打开pdf文件,因为它不完整。如何删除
如果下载不完整,则以编程方式创建文件,并以
该扩展程序已保存在进程中?这是我的代码
当前用于从服务器下载pdf。

class DownloadFileAsync extends AsyncTask<String, String, String>
{
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    final int notify_id = 1;
    final NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    private String resp;

    @Override
    protected String doInBackground(String... params) {


        int count;
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("Download Status...");
        builder.setContentText("Download in Progress...");
        try {
            URL url = new URL(params[0]);
            URLConnection connection = url.openConnection();
            connection.connect();

            int lengthOfFile = connection.getContentLength();
            Log.d("ANDRO_ASYNC", "LENGTH OF FILE : " + lengthOfFile);

            String fileName = params[0].substring(params[0].lastIndexOf('/') + 1, params[0].length());
            Log.d("FILENAME", fileName);
            resp = fileName;

            if (isSDPresent) {

                InputStream inputStream = new BufferedInputStream(url.openStream());
                OutputStream outputStream = new FileOutputStream("sdcard/shatayushi/" + fileName);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lengthOfFile));
                    outputStream.write(data, 0, count);
                }

                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } else {
                InputStream inputStream = new BufferedInputStream(url.openStream());
                OutputStream outputStream = new FileOutputStream(getFilesDir() + "/shatayushi/" + fileName);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lengthOfFile));
                    outputStream.write(data, 0, count);
                }

                outputStream.flush();
                outputStream.close();
                inputStream.close();

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return params[0];
    }

    @Override
    protected void onPostExecute(String filename) {
        Log.d("PARAM", filename);

        String fname = filename.substring(filename.lastIndexOf('/')+1, filename.length());

        int position = Arrays.asList(pdf_url).indexOf(filename);
        Log.d("Position",String.valueOf(position));
        String magazine_id = magazine_names[position];

        if (isSDPresent) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            builder.setProgress(0, 0, false);
            builder.setContentText("Download Complete...");
            NM.notify(notify_id, builder.build());

            TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            String device_imei = telephonyManager.getDeviceId();
            update_info(device_imei,magazine_id);
            Uri uri = Uri.parse("/storage/emulated/0/shatayushi/" + fname);

            Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.setAction(Intent.ACTION_VIEW);

            intent.setData(uri);

            startActivity(intent);

        } else {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            builder.setProgress(0, 0, false);
            builder.setContentText("Download Complete...");
            NM.notify(notify_id, builder.build());

            Uri uri = Uri.parse(getFilesDir() + "/shatayushi/" + fname);

            Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.setAction(Intent.ACTION_VIEW);

            intent.setData(uri);

            startActivity(intent);

        }


    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        Log.d("ANDRO_ASYNC", values[0]);
        progressDialog.setProgress(Integer.parseInt(values[0]));
        builder.setProgress(100, Integer.parseInt(values[0]), false);
        NM.notify(notify_id, builder.build());
    }
}


任何帮助或建议,谢谢。

最佳答案

您只需获取指向文件的指针并使用“ delete()”方法

String fileName = "my_file"; //your file name
File parentFile; //get your parent file
File myFile = new File(parentFile, fileName);
myFile.delete();

10-06 05:47
查看更多