0

I am trying to write a piece of code that stores an image captured by an Android device, stores this image and then displays the image in an ImageView. I found some code on StackOverflow (Low picture/image quality when capture from camera) that had a good reception but I cannot implement this code due to Android saying it cannot resolve symbol 'PICTURE_RESULT'. I am wanting to use this code so that the image can be displayed with better image quality than the lower quality that results when displaying straight from image capture.

Here is how I have implemented the code, which is exactly the same as how it has been described in the post I have linked:

public void launchCamera(View view){
    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From your camera");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    //use standard intent to capture an image
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //output captureIntent to imageUri
    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    //Take a picture and pass results along to onActivityResult
    PICTURE_RESULT = 1;
    startActivityForResult(captureIntent, PICTURE_RESULT);
}

Here is the code for onActivityResult():

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {

            if (PICTURE_RESULT == 1)
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                getContentResolver(), imageUri);
                        imageView.setImageBitmap(thumbnail);
                        URI imageurl = getRealPathFromURI(imageUri);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
    }

And here is the code for getRealPathFromURI():

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Community
  • 1
  • 1
skelto
  • 157
  • 1
  • 11
  • 1
    Hey PICTURE_RESULT is not default constant of android.You need to define it your own.you can define it like int PICTURE_RESULT=1 then compare it in OnActivityResult method that you have supplied code PICTURE_RESULT and your are getting result for same. – Jay Shah May 03 '15 at 15:47
  • What problem you are getting? – Jay Shah May 03 '15 at 17:21
  • Hi @JayShah, thanks for the response. I'm now getting a problem in the onActivityResult() method for the line, URI imageurl = getRealPathFromURI(imageUri); as it says I'm trying to pass in a String, but I've declared imageUri as Uri and not a String. Would you know how to solve this problem? – skelto May 03 '15 at 17:23
  • can you post whole OnActivityResult code? – Jay Shah May 03 '15 at 17:24
  • @JayShah I've now updated my post to include the code for the other two methods and updated the code for launchCamera(), thanks. – skelto May 03 '15 at 17:26

1 Answers1

1

You should use below code.

 try {
        String path = getRealPathFromURI(data.getData());
     } catch (Exception e) {
           e.printStackTrace();
     }

instead of this code.

 try {
     Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
                                getContentResolver(), imageUri);
     imageView.setImageBitmap(thumbnail);
     URI imageurl = getRealPathFromURI(imageUri);
     } catch (Exception e) {
    e.printStackTrace();
     }

Also I noticed you are displaying image in imageview.You should display image from path using image loader or picasso library.Otherwise every time if you will load bitmap then your app will crash with out of memory error.

Jay Shah
  • 732
  • 4
  • 16
  • Thanks for the help but I've had no luck trying to implement the code from the other post. I think I'm going to try a different approach. – skelto May 03 '15 at 17:45