1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
package com.paad.intentcamera;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CameraActivity extends Activity {
private static final int TAKE_PICTURE = 0;
private Uri outputFileUri;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.imageView1);
Button fullPhotoButton = (Button)findViewById(R.id.buttonFullPicture);
fullPhotoButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/**
* Listing 15-23: Requesting a full-size picture using an Intent
*/
// Create an output file.
File file = new File(Environment.getExternalStorageDirectory(),
"test.jpg");
outputFileUri = Uri.fromFile(file);
// Generate the Intent.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// Launch the camera app.
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button photoButton = (Button)findViewById(R.id.buttonPicture);
photoButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivityForResult(
new Intent(MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE);
}
});
}
/**
* Listing 15-24: Receiving pictures from an Intent
*/
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
// Check if the result includes a thumbnail Bitmap
if (data != null) {
if (data.hasExtra("data")) {
Bitmap thumbnail = data.getParcelableExtra("data");
imageView.setImageBitmap(thumbnail);
}
} else {
// If there is no thumbnail image data, the image
// will have been stored in the target output URI.
// Resize the full image to fit in out image view.
int width = imageView.getWidth();
int height = imageView.getHeight();
BitmapFactory.Options factoryOptions = new
BitmapFactory.Options();
factoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(outputFileUri.getPath(),
factoryOptions);
int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(imageWidth/width,
imageHeight/height);
// Decode the image file into a Bitmap sized to fill the View
factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;
Bitmap bitmap =
BitmapFactory.decodeFile(outputFileUri.getPath(),
factoryOptions);
imageView.setImageBitmap(bitmap);
}
}
}
} |
Partager