0

I am unable to compress a file on all devices in java in android studio. It works on some android versions but not on all. I am uploading a profile picture in my application, but to upload large files I am compressing it to so as to easily upload it. But the method which I am using for compressing the file is working on some android versions but not on all.

I've tried many methods, I've resized my picture so as to reduce its size but at last the compressing method suits the most to my code and it was working fine for some devices but not for all.

   private File getBitmapFile(Bitmap reducedBitmap, File file1) {
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + file1.getName());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        reducedBitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
        byte[] bitmapdata = bos.toByteArray();

        Log.e("","         Working:    ");
        try{
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bitmapdata);
            fos.flush();  
            fos.close();
            return file;

        } catch (Exception e){
            e.printStackTrace();
        }
        return file;
    }
4
  • Do you get any exceptions on the devices that don't work? Commented May 17, 2023 at 5:07
  • Possible duplicate of stackoverflow.com/q/65637610/150978 Commented May 17, 2023 at 7:33
  • Since Android 10 you can not save files anywhere where you want in the "external storage directory". Only specific paths are directly accessible. For the other you need to use scoped storage. Commented May 17, 2023 at 7:33
  • Just try a public directory on external storage for pictures and images like DCIM and Pictures. And you should have placed a Toast() in that catch block to inform the user about the failing. Commented May 17, 2023 at 8:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.