问题
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>
回答1:
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
来源:https://stackoverflow.com/questions/10663019/android-activity-to-open-camera-and-upload-an-image-to-a-server