Java 文件未在/data/user/0/[package name]/files目录中找到,但在/data/data/[package name]/files目录中找到
我正在将一个zip文件从服务器下载到我的内部存储器。它被保存到Java 文件未在/data/user/0/[package name]/files目录中找到,但在/data/data/[package name]/files目录中找到,java,android,io,filesystems,android-file,Java,Android,Io,Filesystems,Android File,我正在将一个zip文件从服务器下载到我的内部存储器。它被保存到/data/data/[package name]/files目录中,我可以使用Android Studio中的设备文件浏览器查看它。但是,当我尝试访问它以解压缩它时,会出现以下错误: java.io.FileNotFoundException: thezip.zip: open failed: ENOENT (No such file or directory) 当我打电话时: context.getFileDir(); 它为我
/data/data/[package name]/files
目录中,我可以使用Android Studio中的设备文件浏览器查看它。但是,当我尝试访问它以解压缩它时,会出现以下错误:
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();
}
}
class DownloadFileAsync扩展了AsyncTask{
私人语境;
私有字符串文件名;
公共下载文件异步(上下文,字符串文件名){
this.context=上下文;
this.fileName=文件名;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串背景(字符串…aurl){
下载ZipFile(aurl[0]);
试一试{
unzipFolder(文件名);
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
Log.d(“ANDRO_ASYNC”,progress[0]);
}
@凌驾
受保护的void onPostExecute(字符串未使用){
}
私有void下载ZipFile(字符串链接){
整数计数;
试一试{
URL=新的URL(链接);
URLConnection=url.openConnection();
connection.connect();
int lenghtOfFile=connection.getContentLength();
Log.d(“ANDRO_ASYNC”,“文件长度:“+lenghtOfFile”);
//File File=新文件(context.getFilesDir(),文件名);
File File=新文件(context.getFilesDir(),文件名);
Log.d(“zzzz”,file.toString());
InputStream输入=新的BufferedInputStream(url.openStream());
FileOutputStream输出=新的FileOutputStream(文件);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
}
私有void unzipFolder(字符串zippedFile)引发IOException{
//Log.d(“zzzz”,context.getFilesDir().toString());
字节[]缓冲区=新字节[1024];
ZipInputStream ZipInputStream=新ZipInputStream(新文件输入流(zippedFile));
ZipEntry-ZipEntry=zipInputStream.getNextEntry();
while(zipEntry!=null){
字符串aFile=zipEntry.getName();
File newFile=新文件(context.getFilesDir(),aFile);
//File newFile=新文件(context.getFilesDir(),aFile);
FileOutputStream FileOutputStream=新FileOutputStream(新文件);
内伦;
而((len=zipInputStream.read(buffer))>0){
写入(缓冲区,0,len);
}
fileOutputStream.close();
zipEntry=zipInputStream.getnextery();
}
zipInputStream.closeEntry();
zipInputStream.close();
}
}
您的设备是否有根目录。你需要根访问权限来访问(数据/数据)显示你的代码。我忘了提到我正在使用模拟器,所以我确实有访问权限。这个问题有解决方案吗?