在 /data/user/0/[软件包名称]/files 目录中找不到文件,但在 /data/data/[软件包名称]/files 目录中找到文件

File not found in /data/user/0/[package name]/files directory but is found in /data/data/[package name]/files directory

提问人:topgun741 提问时间:2/5/2018 最后编辑:topgun741 更新时间:2/6/2018 访问量:6016

问:

我正在将 zip 文件从服务器下载到我的内部存储。它被保存到目录中,我可以使用Android Studio中的设备文件资源管理器查看它。但是当我尝试访问它以解压缩它时,我收到以下错误:/data/data/[package name]/files

java.io.FileNotFoundException: thezip.zip: open failed: ENOENT (No such file or directory)

当我打电话时:

context.getFileDir();

它给了我以下目录:

/data/user/0/[package name]/files

这是指向

/data/data/[package name]/files directory

所以我应该有权访问 zip 文件,不是吗?

我在这里错过了什么?我希望能够让任何使用该应用程序的用户能够访问 zip 文件夹中的文件。帮助!

编辑:这是代码:

class DownloadFileAsync extends AsyncTask<String, String, String> {
    private Context context;
    private String fileName;

    public DownloadFileAsync(Context context, String fileName){
        this.context = context;
        this.fileName = fileName;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... aurl) {
        downloadZipFile(aurl[0]);
        try {
            unzipFolder(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    protected void onProgressUpdate(String... progress) {
        Log.d("ANDRO_ASYNC",progress[0]);

    }

    @Override
    protected void onPostExecute(String unused) {

    }

    private void downloadZipFile(String link){
        int count;

        try {

            URL url = new URL(link);
            URLConnection connection = url.openConnection();
            connection.connect();

            int lenghtOfFile = connection.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

           // File file = new File(context.getFilesDir(), fileName);
            File file = new File(context.getFilesDir(), fileName);
            Log.d("zzzz", file.toString());
            InputStream input = new BufferedInputStream(url.openStream());
            FileOutputStream output = new FileOutputStream(file);

            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) {}
    }

    private void unzipFolder(String zippedFile) throws IOException {
            //Log.d("zzzz", context.getFilesDir().toString());

        byte[] buffer = new byte[1024];
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zippedFile));
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null){
            String aFile = zipEntry.getName();
            File newFile = new File(context.getFilesDir(), aFile);
//            File newFile = new File(context.getFilesDir(), aFile);
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);
            int len;
            while ((len = zipInputStream.read(buffer)) > 0){
                fileOutputStream.write(buffer, 0, len);
            }
            fileOutputStream.close();
            zipEntry = zipInputStream.getNextEntry();
        }

        zipInputStream.closeEntry();
        zipInputStream.close();

    }
}
Java Android IO 文件系统 android-file

评论

0赞 Crammeur 2/5/2018
你有root你的设备吗?您需要根访问权限才能访问(数据/数据)
0赞 greenapps 2/5/2018
显示您的代码。
0赞 topgun741 2/6/2018
我忘了说我正在使用模拟器,所以我确实可以访问
3赞 sri 2/27/2021
这个问题有什么解决方案吗?

答: 暂无答案