1

from my previous question's answers I managed to load image from gallery to imageview on a form without any errors. Now I want to compress the image under 150KB and to store it on app cache as PNG format when the form is submitted. At the same time I want to store the image url in SQLite. I think It can be done by getting image name into hidden textview or editview. But I don't have any clue to compress and store the image in cache. following is the code for whatever done so far. plz someone help.

public class NewPetsFragment extends Fragment implements OnClickListener{

    private DBCreater dbCreate;

    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private ImageView img;

    private static final String cacheDir = "/Android/data/com.example.pet/cache/";
    private static final String CACHE_FILENAME = ".cache";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View gv = inflater.inflate(R.layout.new_pet, null);

        Spinner sp = (Spinner) gv.findViewById(R.id.ETPetType); 
        // get reference 
        sp.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_spinner_dropdown_item, petType));

        Button btnSubmit = (Button) gv.findViewById(R.id.ButtonNext);
        btnSubmit.setOnClickListener(this);

        img = (ImageView)gv.findViewById(R.id.ImageView01);

        ((Button) gv.findViewById(R.id.ETPetImg))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                    }
                });

        return gv;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        //Cursor cursor = managedQuery(uri, projection, null, null, null);
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

note: this code only for image loading

Update: I add some cropping option from gallery like this

img = (ImageView)gv.findViewById(R.id.ImageView01);

        ((Button) gv.findViewById(R.id.ETPetImg))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        intent.putExtra("crop", "true");
                        intent.putExtra("aspectX", 0);
                        intent.putExtra("aspectY", 0);
                        intent.putExtra("outputX", 200);
                        intent.putExtra("outputY", 200);
                        intent.putExtra("return-data", true);
                        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                    }
                });

        return gv;

now the app is crashing after crop and returning error as unable to resume activity. how can I fix this?

4
  • Can you tell us what the image file format is? Is it JPEG, BMP, TIFF, ...? Nov 5, 2014 at 23:29
  • I want them to store in png format.. Nov 6, 2014 at 15:41
  • I hould have been more clear. What format is the image originally in? Nov 6, 2014 at 18:12
  • users can load any format of images via gallery Nov 6, 2014 at 21:37

1 Answer 1

1

If you wish to compress images down to 150K bytes or less, and the images can be a variety of formats, I suggest the following:

  1. Convert image to JPEG if it isn't already.
  2. If the image is less than 150K, you are done.
  3. If not, downsample the JPEG image so that it takes up less space. Or, if the image is large (1024 x 1024 perhaps) reduce the size of the image itself.

Here are some Java code links. I found links to code for other languages as well.

Image Conversion: code

Downsampling: code

Just reduce the image size: programs

2
  • from the codes that you provided I can see that image is converting to another format. But my question is reducing the image size and saving them in cache memory. how to do that? Nov 11, 2014 at 17:44
  • I added another link to software which reduces the image size. Downsampling preserves the image size but reduces the space needed. Nov 13, 2014 at 18:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.