1 year ago

#357682

test-img

patel jignesh

Trying to copy image one folder to another folder using contentResolver in android 11 but it create blank image

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

android

android-contentresolver

content-values

0 Answers

Your Answer

Accepted video resources