0

Possible Duplicate:
Calling camera from an activity, capturing an image and uploading to a server

Here is the code i got on internet:

package com.android.imageuploader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.android.imageuploader.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageUploaderActivity extends Activity {
    private static final int REQUEST_CODE = 1;
    private Button button_1;
    public int TAKE_PICTURE = 1;
    private ImageView image_view;
    private Bitmap bitmap;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image_view = (ImageView) findViewById(R.id.result);
        button_1 = (Button) findViewById(R.id.button1);

        button_1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, TAKE_PICTURE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
            try {
                // We need to recyle unused bitmaps
                if (bitmap != null) {
                    bitmap.recycle();
                }
                InputStream stream = getContentResolver().openInputStream(
                        data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();
                image_view.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

this shows a button and an imageView(contains default image initially) and when i click on the button it takes me to the gallery and when i click on any of the images that image is given to the imageView. I have two questions here: 1.how to make the button take me to the camera and when i capture an image 2.upload it directly to a web server

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="pickImage"
        android:text="Button" >
    </Button>

    <ImageView
        android:id="@+id/result"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/wic_logo_small" >
    </ImageView>

</LinearLayout>
Community
  • 1
  • 1
Housefly
  • 4,324
  • 11
  • 43
  • 70

1 Answers1

3

When user clicks the button you need to open a camera through intent e.g.

    public int TAKE_PICTURE =1
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, TAKE_PICTURE);

and in onactivityresult you will get that image which you captured from your camera.

Now you have to upload that image to server

plz go through the following url

Android post Base64 String to PHP

Community
  • 1
  • 1
Sumant
  • 2,775
  • 2
  • 22
  • 30
  • 1 thing i didnt understand...where is the code using the button to be clicked???...here in the code i cant find any button(this is the only class in the app) but when i run itand click on the button which is in the xml file it takes me to the gallery – Housefly May 19 '12 at 07:03
  • you have to edit that xml and add one button & set it's on click listener in which write the above code. – Sumant May 19 '12 at 07:05
  • no...what i mean is the code i pasted in my question works perfectly as the way i explained....but there is no button in the whole code...so how am i able to click it and go to the gallery??? – Housefly May 19 '12 at 07:10
  • can u paste that main.xml file – Sumant May 19 '12 at 07:11
  • see in xml file android:onClick="pickImage" this is written when u click on button it will call "pickImage" method. how that works u can look into http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – Sumant May 19 '12 at 07:23
  • I changed my code....1s chk the question pls...does this work???....i am working on an emulator so cannot test camera – Housefly May 19 '12 at 07:31