我在手机中创建了两个文件夹。一个有一个图像。单击按钮我要从一个文件夹复制到另一个文件夹。我得到一个FileNotFoundException,尽管我检查它是否存在。我已经声明了该权限.Below是代码。请找到我所犯的错误。
Java类:
File file=new File(Environment.getExternalStorageDirectory()+"/File1");
File internalfle=new File(file+"/Bronze.jpg");
File tocopy=new File(Environment.getExternalStorageDirectory()+"/File2");
if (internalfle.exists())
{
if (tocopy.exists())
{
Toast.makeText(getApplicationContext(),"File exists",Toast.LENGTH_SHORT).show();
try {
InputStream fileInputStream=new FileInputStream(internalfle.getAbsolutePath());
OutputStream outputStream=new FileOutputStream(tocopy.getAbsolutePath());
byte[] buffer=new byte[1024];
int len;
while ((len=fileInputStream.read(buffer))>0)
{
outputStream.write(buffer,0,len);
}
fileInputStream.close();
outputStream.close();
Toast.makeText(getApplicationContext(),"Copied",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
{
Toast.makeText(getApplicationContext(),"Does not Exist",Toast.LENGTH_SHORT).show();
}
例外:
06-02 20:06:41.782 23544-23544/com.globemaster.com.test W/System.err: java.io.FileNotFoundException: /storage/emulated/0/File2: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at com.globemaster.com.test.MainActivity$1$override.onClick(MainActivity.java:65)
06-02 20:06:41.792 23544-23544/com.globemaster.com.test W/System.err: at com.globemaster.com.test.MainActivity$1$override.access$dispatch(MainActivity.java)
at com.globemaster.com.test.MainActivity$1.onClick(MainActivity.java)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
06-02 20:06:41.792 23544-23544/com.globemaster.com.test W/System.err: at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 16 more
答案 0 :(得分:2)
引起异常是因为没有名为&#39; File2&#39;的文件。 FileNotFoundException:/ storage / emulated / 0 / File2:open failed:EISDIR(是一个目录)。从它的外观来看,&#39; File2&#39;是一个目录。
您必须在代码中提供有效路径(请注意细微:&#34; /File2/BronzeCopy.jpg" ):
File file=new File(Environment.getExternalStorageDirectory()+"/File1");
File internalfle=new File(file+"/Bronze.jpg");
File tocopy=new File(Environment.getExternalStorageDirectory()+"/File2/BronzeCopy.jpg");
以下代码只是确保文件存在(但它也可以是目录)
if (tocopy.exists())
因此,你想要的是:
if (tocopy.exists() && !tocopy.isDirectory())
您还必须处理&#39; tocopy&#39;路径无法提供可靠的解决方案。