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;
    }
Umesh P
  • 228
  • 2
  • 7
  • Do you get any exceptions on the devices that don't work? – Just another Java programmer May 17 '23 at 05:07
  • Possible duplicate of https://stackoverflow.com/q/65637610/150978 – Robert May 17 '23 at 07: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. – Robert May 17 '23 at 07: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. – blackapps May 17 '23 at 08:18

0 Answers0