0

I have searched but for some answers but may be my fault couldn't find my desired answer.Now below what i am trying:
I am trying to upload an image like of a status or a post or any profile pic.Profile pic will be small and status or any post image will be big. Now what i want to do:

1. I am converting the image to string text and uploading it to datastore and it's limit is 1Mbyte.So i want to check while uploading image that it doesn't cross limit.

2. I want to check that the image is of png format.If it is not then won't upload.Show a Toast.Can i convert image there?? :(

3. If user is uploading image of suppose 700kbyte but the profile pic is small i.e 100kbyte will be enough for profile pic then i can compress the pic to my defined size and then upload it to datastore.It may remain 700kbyte if it is for status image.

I am converting image to string and uploading it to datastore and again converting back to image while showing it in my app.My code:

public static String imageToStringConverter(Bitmap image){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);

    return imageToString;
}

public static Bitmap stringToimageConverter(String imageString){
    byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
    Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
    return bmp;
}

Now the problem i am facing :

1.When i am uploading the image it is taking time.So should i use asynctask while uploading after converting the image to my desired size??

2.When i first enter into my app ,i have shown profile pic i.e if i log into my account it will fetch an image for profile from datastore.But it is taking much time and my log in seems to be lengthy.

Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85

1 Answers1

1

I have solved my problem by reducing the image. Here is my code:

public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {

            case SELECT_PHOTO:
                Uri imageUri;
                try {
                     imageUri = imageReturnedIntent.getData();
                }catch(Exception e){
                    Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
                    return;
                }
                //final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
                //final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                ShrinkBitmapConverter sh = new ShrinkBitmapConverter(getActivity());
                Bitmap selectedImage = null;
                try {
                    selectedImage = sh.shrinkBitmap(imageUri,450,350);
                } catch (Exception e) {
                    Toast.makeText(getActivity(),"Image Not Found",Toast.LENGTH_SHORT).show();
                }
                statusImage = ImageConverter.imageToStringConverter(selectedImage);
                if(statusImage.length()>512000){
                    Toast.makeText(getActivity(),"Image is too big",Toast.LENGTH_LONG).show();
                }else {
                    postImage.setImageBitmap(selectedImage);
                }
        }
    }

ImageConverter.java:

public class ImageConverter {

    public static String imageToStringConverter(Bitmap image){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        String imageToString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
        return imageToString;
    }

    public static Bitmap stringToimageConverter(String imageString){
        byte[] stringTobyte = Base64.decode(imageString, Base64.NO_WRAP);
        Bitmap bmp = BitmapFactory.decodeByteArray(stringTobyte, 0, stringTobyte.length);
        return bmp;
    }

}

ShrinkBitmapConverter.java:

public class ShrinkBitmapConverter {
    Context context;
    public ShrinkBitmapConverter(Context c){
        context=c;
    }

    public Bitmap shrinkBitmap(Uri uri,int width,int height) throws FileNotFoundException {

        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;

        Bitmap bitmap = null;;
        try {
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
                if (heightRatio > widthRatio)
                {
                    bmpFactoryOptions.inSampleSize = heightRatio;
                } else {
                    bmpFactoryOptions.inSampleSize = widthRatio;
                }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri),null,bmpFactoryOptions);

        } catch (Exception e) {

            Toast.makeText(context,"Image Not Found",Toast.LENGTH_SHORT).show();
        }

        return bitmap;
    }
}
Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85