How to move/rename file from internal app storage to external storage on Android?

后端 未结 8 1090
闹比i
闹比i 2020-11-29 02:21

I am downloading files from the internet and saving the streaming data to a temp file in my app\'s internal storage given by getFilesDir().

Once the download is comp

相关标签:
8条回答
  • 2020-11-29 02:46

    Did some trivial modifications to @barmaley's code

    public boolean copyFile(File src, File dst) {
        boolean returnValue = true;
    
       FileChannel inChannel = null, outChannel = null;
    
        try {
    
            inChannel = new FileInputStream(src).getChannel();
            outChannel = new FileOutputStream(dst).getChannel();
    
       } catch (FileNotFoundException fnfe) {
    
            Log.d(logtag, "inChannel/outChannel FileNotFoundException");
            fnfe.printStackTrace();
            return false;
       }
    
       try {
           inChannel.transferTo(0, inChannel.size(), outChannel);
    
       } catch (IllegalArgumentException iae) {
    
             Log.d(logtag, "TransferTo IllegalArgumentException");
             iae.printStackTrace();
             returnValue = false;
    
       } catch (NonReadableChannelException nrce) {
    
             Log.d(logtag, "TransferTo NonReadableChannelException");
             nrce.printStackTrace();
             returnValue = false;
    
       } catch (NonWritableChannelException nwce) {
    
            Log.d(logtag, "TransferTo NonWritableChannelException");
            nwce.printStackTrace();
            returnValue = false;
    
       } catch (ClosedByInterruptException cie) {
    
            Log.d(logtag, "TransferTo ClosedByInterruptException");
            cie.printStackTrace();
            returnValue = false;
    
       } catch (AsynchronousCloseException ace) {
    
            Log.d(logtag, "TransferTo AsynchronousCloseException");
            ace.printStackTrace();
            returnValue = false;
    
       } catch (ClosedChannelException cce) {
    
            Log.d(logtag, "TransferTo ClosedChannelException");
            cce.printStackTrace(); 
            returnValue = false;
    
        } catch (IOException ioe) {
    
            Log.d(logtag, "TransferTo IOException");
            ioe.printStackTrace();
            returnValue = false;
    
    
        } finally {
    
             if (inChannel != null)
    
                try {
    
                   inChannel.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
    
            if (outChannel != null)
                try {
                    outChannel.close();
               } catch (IOException e) {
                    e.printStackTrace();
               }
    
            }
    
           return returnValue;
        }
    
    0 讨论(0)
  • 2020-11-29 02:48

    Internal and external memory is two different file systems. Therefore renameTo() fails.

    You will have to copy the file and delete the original

    0 讨论(0)
  • 2020-11-29 02:51

    To copy files from internal memory to SD card and vice-versa using following piece of code:

    public static void copyFile(File src, File dst) throws IOException
    {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try
        {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        }
        finally
        {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }
    

    And - it works...

    0 讨论(0)
  • 2020-11-29 02:57

    For Move file best way is Renaming it's path with different path and name example:

    File from = new File(Environment.getExternalStorage().getAbsolutePath()+"/kaic1/imagem.jpg");
    File to = new File(Environment.getExternalStorage().getAbsolutePath()+"/kaic2/imagem.jpg");
    from.renameTo(to);
    
    0 讨论(0)
  • 2020-11-29 03:00

    An alternative to the copying using your own function is to use Apache's library's class "FileUtils" , in the function called copyFile :

    FileUtils.copyFile(src, dst, true);
    
    0 讨论(0)
  • 2020-11-29 03:00

    Picture that:

    • This is internal path : pathInternal
    • And this is external path : pathExternal

    Try with this code:

    public void moveIn (String pathInternal, String pathExternal) {
        File fInternal = new File (pathInternal);
        File fExternal = new File (pathExternal);
        if (fInternal.exists()) {
            fInternal.renameTo(fExternal);
        }
    }
    
    0 讨论(0)
提交回复
热议问题