尝试使用Android 11中的contentResolver将图像复制到另一个文件夹,但创建空白映像

发布于 2025-01-18 09:14:22 字数 3848 浏览 2 评论 0原文

我正在处理一个应用程序,其中我在recyclerview中有图像列表,从recyclerview我必须将图像从一个文件夹复制到另一个文件夹,我的代码在许多Android 11和12设备中都可以正常工作,但是在少数设备中,它创建了空白图像的大小。空白映像在字节中,我不明白我的代码

是我用于复制映像的代码

  public static String copyFile(@NonNull final String filePath, String newName, File targetFolder, Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        FileOutputStream fos = null;
        String folderName = targetFolder.getName();
        String relativePath;

        String fileName = new File(filePath).getName();
        String mimeType = getFileType(fileName);
        ArraySet<Uri> uries = new ArraySet<>();
        if (isImage(mimeType)) {
            uries.add(getImageUriFromFile(filePath, context));
        } else {
            uries.add(getVideoUriFromFile(filePath, context));
        }
        if (targetFolder.getPath().contains(Environment.DIRECTORY_PICTURES)) {
            relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;

        } else if (targetFolder.getPath().contains(Environment.DIRECTORY_DCIM)) {
            relativePath = Environment.DIRECTORY_DCIM + File.separator + folderName;
        } else {
            relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;
        }

        try {
            if (isImage(new File(filePath).getName().split("\\.")[1])) {


                Bitmap bitmap = getBitmap(filePath);
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);

                Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

                fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
                if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)) {
                    fos.flush();
                }
                return getPath(imageUri, context);

            } else {


                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/" + mimeType);
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);

                Uri imageUri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);


                fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
                FileInputStream inStream = new FileInputStream(filePath);
                FileChannel inChannel = inStream.getChannel();
                FileChannel outChannel = fos.getChannel();
                inChannel.transferTo(0, inChannel.size(), outChannel);
                inStream.close();
                fos.close();
                return getPath(imageUri, context);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

,我将此方法在线程内部称为下面的代码。 我从线程中调用了此方法,因为我在复制的文件

 new Thread(() -> StorageUtils.copyFileOnAboveQ(
                              file1.getPath(),
                              file2.getName().split("\\.")[0],
                              file2.getParentFile(), context)).start();

代码上进行另一个操作在许多设备上工作正常,但是在某些设备中,它创建了空白图像

i m working on an application where i have list of images inside recyclerview and from recyclerview i have to copy my images from one folder to another folder my code is working fine in many android 11 and 12 device but in few device it creates blank images the size of blank image is in byte i don't understand what is wrong with my code

here is the code i used for copy image

  public static String copyFile(@NonNull final String filePath, String newName, File targetFolder, Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        FileOutputStream fos = null;
        String folderName = targetFolder.getName();
        String relativePath;

        String fileName = new File(filePath).getName();
        String mimeType = getFileType(fileName);
        ArraySet<Uri> uries = new ArraySet<>();
        if (isImage(mimeType)) {
            uries.add(getImageUriFromFile(filePath, context));
        } else {
            uries.add(getVideoUriFromFile(filePath, context));
        }
        if (targetFolder.getPath().contains(Environment.DIRECTORY_PICTURES)) {
            relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;

        } else if (targetFolder.getPath().contains(Environment.DIRECTORY_DCIM)) {
            relativePath = Environment.DIRECTORY_DCIM + File.separator + folderName;
        } else {
            relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;
        }

        try {
            if (isImage(new File(filePath).getName().split("\\.")[1])) {


                Bitmap bitmap = getBitmap(filePath);
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);

                Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

                fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
                if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)) {
                    fos.flush();
                }
                return getPath(imageUri, context);

            } else {


                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
                contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/" + mimeType);
                contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);

                Uri imageUri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);


                fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
                FileInputStream inStream = new FileInputStream(filePath);
                FileChannel inChannel = inStream.getChannel();
                FileChannel outChannel = fos.getChannel();
                inChannel.transferTo(0, inChannel.size(), outChannel);
                inStream.close();
                fos.close();
                return getPath(imageUri, context);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

and i called this method inside thread like below.
i called this method from thread because i m doing another operation on copied file

 new Thread(() -> StorageUtils.copyFileOnAboveQ(
                              file1.getPath(),
                              file2.getName().split("\\.")[0],
                              file2.getParentFile(), context)).start();

code is working fine in many devices but in some device it create blank image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文