简体   繁体   English

阅读下载的文本文件 - Android

[英]Read Downloaded Text File - Android

So my program works like this: It downloads a .txt file from the dropbox and saves it to downloads directory.所以我的程序是这样工作的:它从保管箱下载一个 .txt 文件并将其保存到下载目录。

Uri uri = Uri.parse(file);
DownloadManager.Request r = new DownloadManager.Request(uri);

// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");

r.allowScanningByMediaScanner();

// Start download
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);

And the code is working fine, it downloads the file called upload.txt.并且代码运行良好,它下载名为upload.txt 的文件。 So now i want to read from it.所以现在我想从中阅读。 I have been looking for some codes and nothing seems to be working...我一直在寻找一些代码,但似乎没有任何效果......
Here is my current code, it throws a FileNotFoundException but file is located there.这是我当前的代码,它抛出 FileNotFoundException 但文件位于那里。
Any tips?有小费吗?

String text="";
    FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");
    byte[] b=new byte[100];
    Fin.read(b);
    text=new String(b);
    Fin.close();

Ofc i have tried putting "upload.txt", and even "/upload" and "/upload.txt" but still nothing. Ofc 我试过把“upload.txt”,甚至“/upload”和“/upload.txt”,但仍然没有。
If anyone can give me code that reads a text file, it would be awesome, or if someone could give me a code that can get the source code form a html site ( without JSOUP and other parsers, i have tried that, i want to implement my own parser ).如果有人可以给我读取文本文件的代码,那就太棒了,或者如果有人可以给我一个可以从 html 站点获取源代码的代码(没有 JSOUP 和其他解析器,我已经尝试过了,我想实现我自己的解析器)。 Thanks!!谢谢!!

Change 更改

FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");

to

FileInputStream Fin=new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload"));

You should use Environment.getExternalStoragePublicDirectory to get the path. 您应该使用Environment.getExternalStoragePublicDirectory来获取路径。


Or if you like more, 或者,如果您想要更多,

FileInputStream Fin=new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/upload");

Change 更改

r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");

too with 也是

r.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload");

Took me ages, but finally: You should add to the manifest the various permissions (android.permission.READ_EXTERNAL_STORAGE...)花了我很长时间,但最后:您应该将各种权限添加到清单中(android.permission.READ_EXTERNAL_STORAGE ...)

But this is not enough!但这还不够! you should explicitly ask for these permissions in the onCreate():您应该在 onCreate() 中明确要求这些权限:

requestPermissions({android.permission.READ_EXTERNAL_STORAGE,...}, 200); requestPermissions({android.permission.READ_EXTERNAL_STORAGE,...}, 200);

Only afterwards you can access these directories.只有在此之后您才能访问这些目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM