stanley santoso stanley santoso - 2 years ago 168
Android Question

How to upload image to Parse in android?

I have been able to take a picture using a camera or take from a gallery and show it in an ImageView using this code. What I need to do now is to use that picture and upload it to Parse. I have been googling here and there to do this, and I haven't found the right way to do it. Can someone please help me with this? Is it possible to upload the image from the ImageView? Thank you.

protected Button mFromCamera;
protected Button mFromGallery;
protected ImageView mImageView;

private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PHOTO = 100;

protected void onCreate(Bundle savedInstanceState) {

//Initialize ImageView
mImageView = (ImageView) findViewById(;
//Initialize Camera
mFromCamera = (Button) findViewById(;

//use camera
mFromCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} //use camera end


//initialize button
mFromGallery = (Button) findViewById(;

//pick a photo
mFromGallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
});//pick a photo end

//previewing Image
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//from the gallery
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null!= data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);

//from the camera
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
}//Preview Image End

Answer Source

reading your answer :

I already followed the code that you have before. I was able to upload the image to parse. but I dont know how to switch the drawable source to be my image from camera/gallery or imageview. – stanley santoso

to :

Abhishek Bansal

I understand that your problem is not parsing your image ?

To try to answer your question :

I dont know how to switch the drawable source to be my image from camera/gallery or imageview.

1 - R.drawable.androidbegin seems to be your problem BUT the fact is that you already have your bitmap to parse in your code :

from gallery ->


from camera ->

Bitmap photo = (Bitmap) data.getExtras().get("data");

2 - So I would suggest to declare a variable of type Bitmap at the beginning of your code

private Bitmap yourbitmap;

3 - then assign the bitmap for the gallery and the camera in your code and use it to parse it.

yourbitmap = BitmapFactory.decodeFile(picturePath);
yourbitmap = (Bitmap) data.getExtras().get("data");

4 - finally you can use your bitmap like so :

//    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
//                            R.drawable.androidbegin);
    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    // Compress image to lower quality scale 1 - 100
                    yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] image = stream.toByteArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download