我有问题。我尝试下载pdf文件,然后将此文件保存在我的应用程序文件夹中。但是,当我尝试执行此操作时,出现错误,并且文件未保存...
这是我的提供者
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="Android/data/xxx/files/Download" />
</paths>
我把它放在清单中
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="xxx.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths2" />
</provider>
我这样做:
public static void parseBase64ToPDFAndNoOpen(String base64, String cardId ,Context context) throws IOException {
FileOutputStream os;
String path;
Uri photoURI = null;
try {
photoURI = FileProvider.getUriForFile(context.getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider", createImageFile(context,cardId));
} catch (IOException e) {
e.printStackTrace();
}
File dwldsPath = new File(photoURI.toString());
// File dwldsPath = new File(Environment.getExternalStorageDirectory() + "/" + File.separator + cardId + ".pdf");
if (!dwldsPath.exists()) {
dwldsPath.createNewFile();
byte[] pdfAsBytes = Base64.decode(base64, 0);
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();
}
}
private static File createImageFile(Context context,String name) throws IOException {
File imagesDir = new File(getDefaultTempFilesPath(context));
return File.createTempFile(
name, /* prefix */
".pdf", /* suffix */
imagesDir /* directory */
);
}
private static String getDefaultTempFilesPath(Context context) {
if (context != null) {
File f = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (f != null)
return f.getPath();
else {
f = context.getFilesDir();
if (f != null)
return f.getPath();
}
}
return null;
}
我有:java.io.IOException:没有这样的文件或目录
答案 0 :(得分:0)
答案 1 :(得分:0)
尝试更改这两种方法:
private static File createImageFile(Context context, String name) {
return new File(getDefaultTempFilesPath(context), name + ".pdf");
}
private static File getDefaultTempFilesPath(Context context) {
File f = null;
if (context != null) {
f = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (f == null)
f = context.getFilesDir();
}
return f;
}
还可能需要更改
path="Android/data/xxx/files/Download"
到
path="."
答案 2 :(得分:0)
在xml / path.xml文件中:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
向AndroidManifest.xml添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
添加到AndroidManifest.xml中的应用程序标签:
<provider
android:name=".Modules.ContentProviders"
android:authorities="${applicationId}.provider"
android:exported="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/path" />
</provider>
MainActivity:
DownloadFileFromURL sdsa = new DownloadFileFromURL();
sdsa.execute(downloadurl);
DownloadFileFromURL:
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL as = new URL(f_url[0]);
URLConnection conection = as.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(as.openStream(), 8192);
String diskurl = as.toString().replace(Constants.Download_url, "");
String diskurlSpace = diskurl.replaceAll("%20", " ");
OutputStream output = new
FileOutputStream(Environment.getExternalStorageDirectory() +
"/DocManager" + diskurlSpace);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
Snackbar.make(rayMain, "download and saved",
Snackbar.LENGTH_LONG).show();
}
}