簡體   English   中英

如何在android中將所有圖像從一個文件夾移動到另一個文件夾

[英]How to move all images from one folder to another in android

我是Android的新手,我想將所有圖像文件從一個文件夾(臨時文件夾)移動到另一個文件夾(新文件夾)。 我寫了一些代碼,但我收到了錯誤。(一旦成功移動,該文件應該從臨時文件夾中刪除)請任何人幫助我

這是我的代碼:

String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
                File sourceStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Example/tmp");
                File destStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Example/"+timeStamp);
                try {
                    copyDirectory(sourceStorageDir,destStorageDir);
                } catch (IOException e) {
                    e.printStackTrace();
                }


public void copyDirectory(File sourceLocation , File targetLocation)throws IOException {

   // FileUtils.copyFile(sourceLocation, targetLocation);

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < sourceLocation.listFiles().length; i++) {

            copyDirectory(new File(sourceLocation, children[i]),new File(targetLocation, children[i]));
        }
    } else {
        Log.d("Else", String.valueOf(targetLocation)+"-->"+String.valueOf(sourceLocation));

        InputStream in = new FileInputStream(sourceLocation);

        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}

我的logcat:

04-15 12:04:35.302 21236-21236/com.motopit W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Example/20170415/IMG__&_20170415_104438.jpg (Not a directory)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at java.io.FileOutputStream.open(Native Method)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at com.motopit.ImageActivity.copyDirectory(ImageActivity.java:326)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at com.motopit.ImageActivity.copyDirectory(ImageActivity.java:319)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at com.motopit.ImageActivity.onOptionsItemSelected(ImageActivity.java:285)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.app.Activity.onMenuItemSelected(Activity.java:3208)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:198)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:107)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:671)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:817)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:964)
04-15 12:04:35.302 21236-21236/com.motopit W/System.err:     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:954)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:157)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.view.View.performClick(View.java:5612)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.view.View$PerformClick.run(View.java:22288)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6123)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-15 12:04:35.303 21236-21236/com.motopit W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
04-15 12:04:35.692 21236-21269/com.motopit W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

提前致謝!

試試這個吧。

public void copyFileFromDirectory(File sourceLocation, File targetLocation)
{
    if (sourceLocation.isDirectory())
    {
        if (!targetLocation.exists() && !targetLocation.mkdirs())
        {
            try
            {
                throw new IOException("Directory not creating " + targetLocation.getAbsolutePath());
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++)
        {
            copyFileFromDirectory(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    }
    else
    {
        File directory = targetLocation.getParentFile();
        // Check Directory is exist or not.
        if (directory != null && !directory.exists() && !directory.mkdirs())
        {
            try
            {
                throw new IOException("Directory not creating " + directory.getAbsolutePath());
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(sourceLocation);
            out = new FileOutputStream(targetLocation);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

        byte[] buf = new byte[1024];
        int len;
        try
        {
            while ((len = in.read(buf)) > 0)
            {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM