3

I wrote this code that takes a picture with the camera then converts it to a jpeg file and uploads it to Parse.com as a ParseFile.

The problem is that the picture is really small (153 x 204 px) and really low quality.

I need the picture to be at least 5 MP quality and to crop it to 300x300 px.

Here is the code I use til now.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_picture);

        img = (ImageView) findViewById(R.id.imgV);

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        byte[] image_byte_array;
              ParseObject post_object = new ParseObject("Gallery");
            Bundle extras = data.getExtras();
            Bitmap image = (Bitmap) extras.get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            image_byte_array = stream.toByteArray();
            ParseFile picture_file = new ParseFile("Picture.jpg", image_byte_array);
            picture_file.saveInBackground();
            post_object.put("photo", picture_file);
            post_object.saveInBackground();
        }

Thanks in advance.

drilonrecica
  • 327
  • 1
  • 4
  • 19

2 Answers2

2

Check the link below

http://developer.android.com/guide/topics/media/camera.html#intent-image

You have to add a Uri which is the Uri of file to store the photo

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

a full sample

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_picture);

    img = (ImageView) findViewById(R.id.imgV);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • I do not want to save the picture to my phone storage, I just need it to be uploaded directly to the Parse.com server with the code that I have shown, so if you have any idea I would appreciate it to know what I'm doing wrong. – drilonrecica Sep 04 '15 at 17:06
  • By using Camera Intent, you cannot avoid saving the picture to storage. If you really want to skip saving to storage, you have to implement your own camera. – Derek Fung Sep 04 '15 at 17:09
  • The `data` returned in `onActivityResult` is only a thumbnail, that's why it is so small. – Derek Fung Sep 04 '15 at 17:12
  • ok thank you , the answer that @Darpan linked helped me a lot , still thanks ;) – drilonrecica Sep 04 '15 at 17:50
2

Check this answer here.

A camera intent return a thumbnail by default, so you need to fetch the full image.

Derek's solution is right, but you have to improve it.

the real juice lies in fetching the full image which happens here -

thumbnail = MediaStore.Images.Media.getBitmap(
                            getContentResolver(), imageUri);
                    imgView.setImageBitmap(thumbnail);
                    imageurl = getRealPathFromURI(imageUri);    

full path will be given by getRealPathFromURI().

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • thank you , Ill select your answer as the right one , because the answer you linked helped me , thanks ;) – drilonrecica Sep 04 '15 at 17:51
  • A tip - Try to google a bit more. In android, unless it is a very new API; all the question have been answered already. Such a great support base for us :) – Darpan Sep 05 '15 at 05:51