无法使用asynctask

时间:2018-01-10 15:47:04

标签: java android image file android-asynctask

我创建了一个图片库应用。

我的要求:我想选择多个图片,点击按钮剪切并返回显示所有文件夹的活动(ImageGallery.java)。现在,我想在选择文件夹时选择一个文件夹并粘贴该文件夹中的所有选定图像。

发生了什么?我可以使用我的应用程序选择图像并返回显示所有文件夹但无法使用我的应用程序移动它们的活动。 我使用任务将代码移动到后台线程中。我从一个文件夹中选择图像,返回显示所有文件夹(ImageGallery.java)的活动,然后选择要移动图像的文件夹。但是当我尝试移动图像时,选择图像时,所选图像不会移动到正在选择的其他文件夹。我猜AsyncTask中的代码甚至没有被执行。

我该如何解决?

PhotosActivity.java (用于选择图片的活动):

int int_position;
private GridView gridView;
GridViewAdapter adapter;
ArrayList<Model_images> al_menu = new ArrayList<>();
private ArrayList<Integer> mSelected = new ArrayList<>();
boolean boolean_folder;

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.setVisibility(View.VISIBLE);
            button2.setVisibility(View.VISIBLE);
            button3.setVisibility(View.VISIBLE);
            button4.setVisibility(View.VISIBLE);
            button5.setVisibility(View.VISIBLE);
            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.putIntegerArrayListExtra("selected_images", mSelected);
                            startActivity(moveIntent);
                        }
                    });

ImageGallery.java

public static ArrayList<Model_images> al_images = new ArrayList<>();
ArrayList<Integer> selectedImages = new ArrayList<>();
boolean boolean_folder;
Adapter_PhotosFolder obj_adapter;
GridView gv_folder;
private static final int REQUEST_PERMISSIONS = 100;
int int_position;

selectedImages = getIntent().getIntegerArrayListExtra("selected_images");

if (selectedImages != null) {
    Toast.makeText(ImageGallery.this, "This code gets executed", Toast.LENGTH_SHORT)
            .show();
    new LongOperation().execute();
}

private class LongOperation extends AsyncTask<String, Void, String> {   
    @Override
    protected String doInBackground(String... params) {

        for (int image : selectedImages) {

            File sourceImage = new File(al_images.get(int_position).getAl_imagepath().get(image)); //returns the image File from model class to be moved.
            File destinationImage = new File(al_images.get(int_position).getStr_folder(), ".jpeg");

            try {
                copyOrMoveFile(sourceImage, destinationImage, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //Method to move the file
    private void copyOrMoveFile(File file, File dir, boolean isCopy) throws IOException {
        File newFile = new File(dir, file.getName());
        FileChannel outChannel = null;
        FileChannel inputChannel = null;
        try {
            outChannel = new FileOutputStream(newFile).getChannel();
            inputChannel = new FileInputStream(file).getChannel();
            inputChannel.transferTo(0, inputChannel.size(), outChannel);
            inputChannel.close();
            if (!isCopy)
                file.delete();
        } finally {
            if (inputChannel != null) inputChannel.close();
            if (outChannel != null) outChannel.close();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您必须使用Intent.ACTION_MEDIA_SCANNER_SCAN_FILE来更新媒体商店。

Inside AsyncTask - &gt; onPostExecute方法从 MediaStore

获取最新图像
private class LongOperation extends AsyncTask<String, Void, File> {

        @Override
        protected File doInBackground(String... params) {

            for (String imagePath : selectedImages) {
                File sourceImage = new File(imagePath); //returns the image File from model class to
                // be// moved.
                File destinationImage = new File(al_images.get(int_position).getDirectoryPath() +
                        File.separator + sourceImage.getName());

                try {
                    moveFile(sourceImage, destinationImage, true);
                    return destinationImage;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onPostExecute(File file) {
            super.onPostExecute(file);
            getBaseContext().sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    fn_imagespath(); // Call method to fetch latest images.
                }
            }, 1000); // additional delay time of 1 sec to update media scanner
        }
    }

移动文件

的更好方法
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();
            }
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }

        }
    }

答案 1 :(得分:1)

添加的代码X没有太大变化。试试看 。

MediaScannerConnection