5

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;

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


//Initialize ImageView
mImageView = (ImageView) findViewById(R.id.ImgPrev);
//Initialize Camera
mFromCamera = (Button) findViewById(R.id.FromCamera);

//use camera
mFromCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    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(R.id.FromGallery);

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



//previewing Image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
    //from the gallery
    case SELECT_PHOTO:
        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);
            cursor.moveToFirst();

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

            mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
        break;
    //from the camera
    case CAMERA_REQUEST:
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            mImageView.setImageBitmap(photo);
        }
        break;
}
}//Preview Image End
stanley santoso
  • 343
  • 1
  • 6
  • 19

3 Answers3

3

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 ->

mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

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();
           ...
mourad
  • 667
  • 4
  • 11
  • Thank you very much. this works. I didn't realize that those are the source of my images since the format looks very different. I was able to put the uploading code under the case inside the onActivityForResult at the bottom. Is it possible to do it outside onActivityForResult? I think I need to do it in onCreate so I can parse other information(username and stuffs) in one click so they go to the same object/class. and this code means, if i picked an image from a gallery, then changed my mind to take a picture instead, I will be uploading 2 images. How do I do it so I only upload 1 image? – stanley santoso Jul 05 '15 at 08:17
  • hi, yes you can use the uploading code in its own function, then you can reference it from wherever you like. For example : private myUploading() { // 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(); ... } And in your onActivityResult before each break; you make a call to the uploading with : myUploading(); Voilà. – mourad Jul 05 '15 at 11:52
  • Also was my answer the one ? I wanted so much the green mark. – mourad Jul 05 '15 at 13:49
  • Thank you for the advice. But, I got totally lost trying to understand it. When I bring the code in onCreate, most of the references doesn't work since they are down below in onActivityForResult. and I cannot bring onActivityForResult to onCreate. Can you give me a sample code? – stanley santoso Jul 05 '15 at 18:31
  • also, it seems like I need to put something inside the brackets () when putting "private void myUploading(){...}" in onCreate. What do I put there? and how do I call myUploading()? writting just "myUploading" doesn't seem to do the work. – stanley santoso Jul 05 '15 at 18:41
  • ok stanley, it is difficult to show you in comments due to limited formatting but i'll try to explain : 1) you need to write private void myUploading(){...} outside of the onCreate, it is a separate method. You can put it after the method onActivityResult{...} for example. Then when you need to call the method myUploading you write : myUploading(); and that's it. And one thing, sorry to insist but you did not give me the green mark, you gave it to Abhishek Bansal. – mourad Jul 05 '15 at 19:38
  • I'm sorry about the green mark. I gave both of your the green mark earlier. I might have accidentally pressed on it again and cancelled my earlier action. I've returned your green mark :(.. I just realised I can only give one green mark. my bad. it's yours. – stanley santoso Jul 05 '15 at 19:58
  • Thank you. the function works. So the way I use myUploading is inside on onClick button that parses other information such as name and age. and when that button is clicked, it uploaded the image and the information. But they are created as seperate entries. 1 entry for all the informations and another 1 for just the image. how do I combine this so they become 1 entry? – stanley santoso Jul 05 '15 at 20:09
  • thank you stanley for the green mark, much appreciated. Now for your question : you can add parameters to the function myUploading() {...}. For example you can add myUploading(String myname, int myage) { ... } so the function will do it all : upload the image, plus name and age. Then when you call the new myUploading() function, you need to call myUploading(name, age); – mourad Jul 05 '15 at 20:18
  • IT WORKS! HURRAYY!!! Thank you very much. It now goes uploaded as 1 entry. But now it takes like 2 minutes to process. Why does it take so long? it used to take like 5 seconds. – stanley santoso Jul 05 '15 at 20:34
  • great stanley, i think you need to open a new thread/question as the original has be resolved. Thank you for your accepted answer. – mourad Jul 05 '15 at 20:44
2

there are good tutorials available on internet. Essentially following is what you need to do

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                        R.drawable.androidbegin);
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

                // Create the ParseFile
                ParseFile file = new ParseFile("androidbegin.png", image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("ImageUpload");

                // Create a column named "ImageName" and set the string
                imgupload.put("ImageName", "AndroidBegin Logo");

                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

source: this tutorial

also see this question How to upload an image in parse server using parse api in android

Community
  • 1
  • 1
Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
  • 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 Jul 05 '15 at 06:18
0

Click here to get AsyncHttpClient library and upload your image. it is fats to upload your image.

public void uploadImage(Bitmap img_bit)
{
    AsyncHttpClient imgupload = new AsyncHttpClient();
    RequestParams params = new RequestParams();
        if (img_bit != null) {

            byte[] imagebyte;
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            img_bit.compress(Bitmap.CompressFormat.PNG, 100, bao);
            imagebyte = bao.toByteArray();
            params.put("image", new ByteArrayInputStream(imagebyte),   "test"+ System.currentTimeMillis() + ".png");
        }
        imgupload.post("url",params, new AsyncHttpResponseHandler() {

                            @Override
                            public void onSuccess(int arg0, Header[] arg1,
                                    byte[] arg2) {
                                System.out.println("Image Upload successfully");
                            }

                            @Override
                            public void onFailure(int arg0, Header[] arg1,
                                    byte[] arg2, Throwable arg3) {
                                System.out.println("faile the data");
                            }
                        });


     }
jinkal
  • 1,622
  • 16
  • 21