我写了一些代码,用于使用我创建的图库库应用程序将多个图像从一个文件夹移动到另一个文件夹。我可以选择多个图像,但一次只能移动一个图像
多个图像被选择到arraylist,但运动图像的for循环只运行一次。
多重选择的for循环正常运行,并返回正确选择的图像数量
在Asynctask中运行的for循环有问题。我无法弄清楚为什么它只运行一次。它在doinBackground()
上的ImagesGallery.java
AsyncTask中运行
我该如何解决这个问题?
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();
}
}
}
答案 0 :(得分:1)
只移动一个图像的原因是您在第一次使用return语句执行时打破了for循环。
要解决这个问题,请使用以下
替换AsyncTask<script>