簡體   English   中英

為什么我的Android不讀取“下載”文件夾中的該文件?

[英]Why isn't my android reading this file in Downloads folder?

我的下載文件夾中有一個名為“ mysimulation.txt”的文件。

            Scanner input = new Scanner(System.in);
            String filename = "/mysimulation.txt";

            try {
                input = new Scanner(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + filename));
                Toast.makeText(this, "found", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                Toast.makeText(this, "not found " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), Toast.LENGTH_LONG).show();
            }

為什么我的手機無法讀取“下載”文件夾中的該文件? 我收到未找到文件的異常。

對於任何大於android 6.0(masshmallow)的android。 將以下代碼添加到您的Java代碼中以授予讀寫權限:

File file = new File(path);
 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                            123);
 //Check if the file is writable or readable (.canRead()) 
 Toast.makeText(this, file.canWrite() ? "y" : "no ", Toast.LENGTH_SHORT).show();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM