0

I'm trying to resize the captured image so that it can has a size smaller than 1Mb. This is what I'm tried so far in OnActivityResult but failed. I'm get the tutorial from Android take photo and resize it before saving on sd card

ImageFitScreen.java

 try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapOptions.inJustDecodeBounds = false;
                    bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                    bitmapOptions.inDither = true;
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                    Global.img = bitmap;

                    b.setImageBitmap(bitmap);
                    String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
                    //p = path;
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");
                    try {

                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);
                        //pic=file;
                        outFile.flush();
                        outFile.close();


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

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

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

Claims.java

 button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
                   // Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
                    byte[] data=getBitmapAsByteArray(getActivity(),Global.img);// this is a function
                    Toast.makeText(getActivity().getApplicationContext(), data+"", Toast.LENGTH_LONG).show();

                    if(data==null)
                    {
                        Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
                    }
                    else
                    {


                        Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
                        SB.insertStaffBenefit(name, data, description, result, fk);
                    }

        });
        return claims;
    }

    public static byte[] getBitmapAsByteArray(final Context context,Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        Toast.makeText(context, outputStream.size()/1024+"KB", Toast.LENGTH_LONG).show();
        return outputStream.toByteArray();
    }

In Claims.java, it still display the size which is more than 1Mb

Can someone help me? Thanks

Community
  • 1
  • 1
  • Please explain, **completely and precisely**, what "failed" means. For example, are you crashing? If so, use LogCat to examine your Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 10 '15 at 15:22
  • @CommonsWare I'm so sorry. I have edited my post. –  Nov 10 '15 at 15:28
  • is this the correct way to implement? Or I implemented in the wrong places? –  Nov 10 '15 at 15:32

1 Answers1

0

Check out the link below

How to resize Image in Android?

For better UI give toast to the user asking to upload image of specific size still he uploads larger image check it in backend or server and give negative response or prompt hi to again upload image of smaller size.

Community
  • 1
  • 1
akshay toshniwal
  • 180
  • 1
  • 11