Java 无法一次将多个图像移动到另一个文件夹
我编写了一些代码,使用我创建的图像库应用程序将多个图像从一个文件夹移动到另一个文件夹。我可以选择多个图像,但一次只能移动一个图像。 在arraylist中选择多个图像,但运动图像的for循环仅运行一次。 多重选择的for循环运行正常,并返回所选图像的正确数量。 Asynctask内部运行的for循环有问题。我无法理解为什么它只运行一次。它在ImagesGallery.java上AsyncTask的doinBackground中运行 我该如何解决这个问题 PhotosActivity.java: ImageGallery.java:Java 无法一次将多个图像移动到另一个文件夹,java,android,for-loop,android-asynctask,Java,Android,For Loop,Android Asynctask,我编写了一些代码,使用我创建的图像库应用程序将多个图像从一个文件夹移动到另一个文件夹。我可以选择多个图像,但一次只能移动一个图像。 在arraylist中选择多个图像,但运动图像的for循环仅运行一次。 多重选择的for循环运行正常,并返回所选图像的正确数量。 Asynctask内部运行的for循环有问题。我无法理解为什么它只运行一次。它在ImagesGallery.java上AsyncTask的doinBackground中运行 我该如何解决这个问题 PhotosActivity.java:
之所以只移动一个映像,是因为在第一次使用return语句执行for循环时,您正在中断该循环 要解决此问题,请使用以下命令替换AsyncTask
public class LongOperation extends AsyncTask<Void, Void, Void> {
@Override
public Void doInBackground(Void... voids) {
for (String imagePath : selectedImages) { //this is the second for loop which might be running only once
File sourceImage = new File(imagePath); //returns the image File from model class to
// be// moved.
File destinationImage = new File(al_images.get(id).getDirectoryPath() +
File.separator + sourceImage.getName());
try {
moveFile(sourceImage, destinationImage, false);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private void moveFile(File file_Source, File file_Destination, boolean isCopy) throws IOException {
FileChannel source = null;
FileChannel destination = null;
if (!file_Destination.exists()) {
file_Destination.createNewFile();
}
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
long count = 0;
long size = source.size();
while ((count += destination.transferFrom(source, count, size - count)) < size) ;
if (!isCopy) {
file_Source.delete();
MediaScannerConnection.scanFile(this,
new String[] { file_Source.toString() }, null,null);
}
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
选择多个图像后,图像列表的大小是多少?其大小等于所选图像的数量@Tej我已经更新了问题Post您的moveFile方法。完成@chaitanyaatkuriw您为什么使用return destinationImage;代码?移动图像后为什么不更新视图?@Amelia你能告诉我哪一行是315号吗。MediaScannerConnection.scanFilecontext,@Amelia OK。尝试将MediaScannerConnection.scanFilethis替换为MediaScannerConnection.scanFileYourActivity.this。还要检查源文件是否为null。如果这样做,源文件就不为null,否则我认为它也无法处理以前的代码段
ArrayList<String> selectedImages = new ArrayList<>();
if (getIntent().getSerializableExtra("selected_images") != null)
selectedImages = (ArrayList<String>) getIntent().getSerializableExtra("selected_images");
gv_folder.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected element
view.setBackgroundColor(Color.LTGRAY);
buttoncut.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
button4.setVisibility(View.VISIBLE);
button5.setVisibility(View.VISIBLE);
button2.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
buttoncut.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
button3.setVisibility(View.GONE);
button4.setVisibility(View.GONE);
button5.setVisibility(View.GONE);
buttonpaste.setVisibility(View.VISIBLE);
new LongOperation(i).execute();
}
});
private class LongOperation extends AsyncTask<String, Void, File> {
@Override
protected File doInBackground(String... params) {
for (String imagePath : selectedImages) { //this is the second for loop which might be running only once
File sourceImage = new File(imagePath); //returns the image File from model class to
// be// moved.
File destinationImage = new File(al_images.get(id).getDirectoryPath() +
File.separator + sourceImage.getName());
try {
moveFile(sourceImage, destinationImage, false);
return destinationImage;
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private void moveFile(File file_Source, File file_Destination, boolean isCopy) throws IOException {
FileChannel source = null;
FileChannel destination = null;
if (!file_Destination.exists()) {
file_Destination.createNewFile();
}
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
long count = 0;
long size = source.size();
while ((count += destination.transferFrom(source, count, size - count)) < size) ;
if (!isCopy) {
file_Source.delete();
MediaScannerConnection.scanFile(this,
new String[] { file_Source.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
public class LongOperation extends AsyncTask<Void, Void, Void> {
@Override
public Void doInBackground(Void... voids) {
for (String imagePath : selectedImages) { //this is the second for loop which might be running only once
File sourceImage = new File(imagePath); //returns the image File from model class to
// be// moved.
File destinationImage = new File(al_images.get(id).getDirectoryPath() +
File.separator + sourceImage.getName());
try {
moveFile(sourceImage, destinationImage, false);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private void moveFile(File file_Source, File file_Destination, boolean isCopy) throws IOException {
FileChannel source = null;
FileChannel destination = null;
if (!file_Destination.exists()) {
file_Destination.createNewFile();
}
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
long count = 0;
long size = source.size();
while ((count += destination.transferFrom(source, count, size - count)) < size) ;
if (!isCopy) {
file_Source.delete();
MediaScannerConnection.scanFile(this,
new String[] { file_Source.toString() }, null,null);
}
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}