Android 无法将图像复制到另一个文件夹

Android unable to copy image to another folder

我在我的 phone.One 中创建了两个文件夹 有一个 image.Upon 单击按钮 我想从一个文件夹复制到 another.I 得到一个 FileNotFoundException 尽管我检查它是否 exists.I 已声明权限为 well.Below 是 code.Please 发现我犯的错误。

Java Class:

 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$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$override.access$dispatch(MainActivity.java)
        at com.globemaster.com.test.MainActivity.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

异常是因为没有名为'File2'的文件引起的 FileNotFoundException: /storage/emulated/0/File2: open failed: EISDIR (Is a directory).看起来,'File2'是一个目录。

您必须在您的代码中提供有效路径(注意细微之处:"/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()) 

您还必须处理 'tocopy' 路径对于提供可靠解决方案无效的情况。