我是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.
提前致谢!
答案 0 :(得分:0)
试试这个。
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();
}
}
}