gpt4 book ai didi

java - 如果在 Android 中下载不完整,如何删除 pdf?

转载 作者:行者123 更新时间:2023-12-01 09:37:19 25 4
gpt4 key购买 nike

我正在从我的服务器下载一个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();

关于java - 如果在 Android 中下载不完整,如何删除 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38768239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com