gpt4 book ai didi

java - 无法一次将多个图像移动到另一个文件夹

转载 作者:行者123 更新时间:2023-12-02 11:43:49 25 4
gpt4 key购买 nike

我编写了一些代码,使用我创建的图像库应用程序将多个图像从一个文件夹移动到另一个文件夹。我可以选择多张图像,但一次只能移动一张图像。
多个图像被选择到数组列表中,但移动图像的 for 循环仅运行一次。
用于多重选择的 for 循环运行正常,并且返回选择的图像的正确数量。
Asynctask 内运行的 for 循环有问题。我无法弄清楚为什么它只运行一次。它在 ImagesGallery.java 上的 AsyncTask 的 doinBackground() 中运行我该如何解决这个问题?

PhotosActivity.java:

 private ArrayList<Integer> mSelected = new ArrayList<>();

gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
if (mSelected.contains(position)) {
mSelected.remove(position);
view.setBackgroundColor(Color.TRANSPARENT);// remove item from list
// update view (v) state here
// eg: remove highlight
} else {
mSelected.add(position);
view.setBackgroundColor(Color.LTGRAY);// add item to list
// update view (v) state here
// eg: add highlight
}

buttoncut.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);
Intent moveIntent = new Intent(PhotosActivity.this, ImageGallery.class);
moveIntent.putExtra("selected_images", getImagePaths(mSelected));
moveIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(moveIntent);
finish();
}
});

private ArrayList<String> getImagePaths(ArrayList<Integer> selectedIndexList) {
ArrayList<String> listOfImages = new ArrayList<>();
for(Integer index : selectedIndexList) {
listOfImages.add(al_images.get(int_position).getAl_imagepath().get(index)); //images get added to arraylist here
}
return listOfImages;
}

ImageGallery.java:

 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();
}
}
}

最佳答案

仅移动一个图像的原因是您在第一次使用 return 语句执行 for 循环时破坏了 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();
}
}


}

关于java - 无法一次将多个图像移动到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com