Android 未找到 FileInputStream 异常文件
Android FileInputStream exception file not found
我正在尝试用 jtar 取消 tar 一个 tar 并且我得到
java.io.FileNotFoundException: Download/Data.tar (No such file or directory)
我没有使用模拟器,它直接在我的 phone 上。
该文件确实存在于我的 android 的下载文件夹中(甚至在应用程序 tarts 之前)。
我拥有read/write的所有权限。
File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
if(!destFolder.exists())
{
// Make it, if it doesn't exit
destFolder.mkdirs();
}else{
//TODO
//delete the folder contents
}
// Create a TarInputStream
try {
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
TarEntry entry;
while ((entry = tis.getNextEntry()) != null) {
int count;
byte data[] = new byte[2048];
FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos);
while ((count = tis.read(data)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
tis.close();
}catch (Exception e){
Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
toast2.show();
Log.e("TAG", "error", e);
}
Logcat 表示错误在 try
之后的第一行
编辑: 由于某种原因,它甚至没有创建 /My Data/ 文件夹,即使它在我调试时确实转到了 destFolder.mkdirs();
。不确定它是如何关联的
编辑 2: 我发现 tarFile.exists()
和 tarFile.canRead()
说的是假的,我试图把一个明确的路径 "storage/emulated/0/Download/Data.tar"
和它有效,它存在且可读。
这样放着有什么危险吗?有些 phone 会有不同的 ...emullated/X/...
号码吗?
如果您的设备android版本大于6.0,仅在AndroidManifestxml中声明权限请求是不够的。您还应该在任何需要许可的操作之前明确请求。
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
}
}
我解决了!
File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
将尝试从您的应用程序获取相对路径,即使您将编写 tarFile.getAbsolutePath()
。
解决办法?
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");
我正在尝试用 jtar 取消 tar 一个 tar 并且我得到
java.io.FileNotFoundException: Download/Data.tar (No such file or directory)
我没有使用模拟器,它直接在我的 phone 上。
该文件确实存在于我的 android 的下载文件夹中(甚至在应用程序 tarts 之前)。
我拥有read/write的所有权限。
File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
File destFolder = new File(Environment.DIRECTORY_DOCUMENTS + "/My Data/");
if(!destFolder.exists())
{
// Make it, if it doesn't exit
destFolder.mkdirs();
}else{
//TODO
//delete the folder contents
}
// Create a TarInputStream
try {
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
TarEntry entry;
while ((entry = tis.getNextEntry()) != null) {
int count;
byte data[] = new byte[2048];
FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos);
while ((count = tis.read(data)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
tis.close();
}catch (Exception e){
Toast toast2 = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
toast2.show();
Log.e("TAG", "error", e);
}
Logcat 表示错误在 try
之后的第一行编辑: 由于某种原因,它甚至没有创建 /My Data/ 文件夹,即使它在我调试时确实转到了 destFolder.mkdirs();
。不确定它是如何关联的
编辑 2: 我发现 tarFile.exists()
和 tarFile.canRead()
说的是假的,我试图把一个明确的路径 "storage/emulated/0/Download/Data.tar"
和它有效,它存在且可读。
这样放着有什么危险吗?有些 phone 会有不同的 ...emullated/X/...
号码吗?
如果您的设备android版本大于6.0,仅在AndroidManifestxml中声明权限请求是不够的。您还应该在任何需要许可的操作之前明确请求。
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
}
}
我解决了!
File tarFile = new File(Environment.DIRECTORY_DOWNLOADS, "Data.tar");
将尝试从您的应用程序获取相对路径,即使您将编写 tarFile.getAbsolutePath()
。
解决办法?
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Data.tar");