首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何下载并存储在Android 10文件夹中的pdf文件?

如何下载并存储在Android 10文件夹中的pdf文件?
EN

Stack Overflow用户
提问于 2019-12-24 05:59:37
回答 3查看 4.2K关注 0票数 0

直到Android派,我的代码才能正常工作。但是当我更新到Android 10时,它会显示以下错误。怎么解决呢?

E/SubsamplingScaleImageView:未能初始化位图解码器java.io.FileNotFoundException:打开失败: ENOENT (没有此类文件或目录)在android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:315) at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:220) at com.pdfview.PDFRegionDecoder.init(PDFRegionDecoder.kt:25) at com.pdfview.subsamplincscaleimageview.SubsamplingScaleImageView$TilesInitTask.doInBackground(SubsamplingScaleImageView.java:1564) at com.pdfview.subsamplincscaleimageview.SubsamplingScaleImageView$TilesInitTask.doInBackground( android.os.AsyncTask$3.call(AsyncTask.java:378) ) java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:919)

我正在使用以下代码。

代码语言:javascript
复制
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "tmf");
folder.mkdir();

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(folder + "/contract.pdf");

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();`
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-24 06:15:18

试试下面的代码。

代码语言:javascript
复制
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "filename.pdf";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);

String url = downloadurl; //paste url here

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading....");
request.setTitle(" TITLE ");
request.setDestinationUri(uri);

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);

final String finalDestination = destination;
BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Log.d("Update status", "Download completed");
       unregisterReceiver(this);
    }
};

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
票数 0
EN

Stack Overflow用户

发布于 2019-12-24 06:49:11

如果您的应用程序正在访问Q,则不能直接向外部公共存储写入。您必须使用MediaStore将文件保存到一个可公开访问的位置。

您可以使用以下引用代码来使用MediaStore存储文件。请注意,您需要android.permission.WRITE_EXTERNAL_STORAGE来使用MediaStore存储文件。

代码语言:javascript
复制
    val cv = ContentValues().apply {
        put(MediaStore.Files.FileColumns.DISPLAY_NAME, "mypdf.pdf")
    }
    val uri = contentResolver.insert(MediaStore.Files.getContentUri("pdfs"), cv)

    uri?.let { 
        val out = contentResolver.openOutputStream(it)
    }
票数 1
EN

Stack Overflow用户

发布于 2019-12-24 06:16:55

您是否获得了读取外部存储的权限,也没有找到错误状态文件,请确保正在创建该文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59464452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档