Camera code2 : push an intent to default camera application to make it take a photo, save it and return an URI

push an intent to default camera application to make it take a photo, save it and return an URI.

TestCamera04Activity.java
[sourcecode language=”css”]
package test.testcamera04;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TestCamera04Activity extends Activity {
/** Called when the activity is first created. */
TextView textSavePic ;
private Uri imageUri;

private static final int TAKE_PHOTO_CODE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textSavePic = (TextView)findViewById(R.id.textView1);
}

public void CameraOnclick(View v){

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
// startActivityForResult(intent, 0);
startActivityForResult(intent, TAKE_PHOTO_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case TAKE_PHOTO_CODE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
ContentResolver cr = getContentResolver();
Bitmap bitmap;

try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);

textSavePic.setText(selectedImage.toString());
Toast.makeText(this, selectedImage.toString(),Toast.LENGTH_LONG).show();
} catch (Exception e) {
textSavePic.setText(e.toString());
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
}
}
}
}

}
[/sourcecode]

main.xml
[sourcecode language=”css”]
<?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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Camera" />

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

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</LinearLayout>
[/sourcecode]

SS
1)

2)

3)

ref:
http://stackoverflow.com/questions/2729267/android-camera-intent
http://stackoverflow.com/questions/5395674/camera-intent-android

(Visited 13 times, 1 visits today)
Spread the love