这篇文章主要介绍的是调用相机程序获得图片,调用Android操作系统自带的图片处理程序处理图片然后返回到我们的程序。
先看看布局文件:
- <?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:text="Button" />
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
<?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:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
向布局中加入了一个Button和一个ImageView。
在Button的OnClick方法中启动相机程序,让用户选择使用哪个相机,代码如下:
- @Override
- public void onClick(View v) {
- //doTakePhoto();
- File fos=null;
- try {
- fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- } catch (Exception e) {
- e.printStackTrace();
- }
- Uri u=Uri.fromFile(fos);
- Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
- i.putExtra(MediaStore.EXTRA_OUTPUT, u);
- this.startActivityForResult(i, 9);
- }
@Override
public void onClick(View v) {
//doTakePhoto();
File fos=null;
try {
fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
} catch (Exception e) {
e.printStackTrace();
}
Uri u=Uri.fromFile(fos);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
i.putExtra(MediaStore.EXTRA_OUTPUT, u);
this.startActivityForResult(i, 9);
}
效果如下:
向Intent中放入一些信息,指定保存原图,额外的保存在我指定的地址。使用this.startActivityForresult();传入intent对象和请求的值。
覆盖父类的onActivityResult方法,当其他Activity结束之后返回到本Activity。
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(resultCode==RESULT_OK)
- {
- if(requestCode==9)
- {
- File bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- Intent i=new Intent("com.android.camera.action.CROP");
- i.setType("image/*");
- //i.putExtra("data", bb);
- i.setDataAndType(Uri.fromFile(bb), "image/jpeg");
- i.putExtra("crop", "true");
- i.putExtra("aspectX", 1);
- i.putExtra("aspectY", 1);
- i.putExtra("outputX", 500);
- i.putExtra("outputY", 500);
- i.putExtra("return-data", true);
- this.startActivityForResult(i, 7);
- }
- if(requestCode==7)
- {
- Bitmap bb=data.getParcelableExtra("data");
- i.setImageBitmap(bb);
- }
- }
- }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK)
{
if(requestCode==9)
{
File bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
Intent i=new Intent("com.android.camera.action.CROP");
i.setType("image/*");
//i.putExtra("data", bb);
i.setDataAndType(Uri.fromFile(bb), "image/jpeg");
i.putExtra("crop", "true");
i.putExtra("aspectX", 1);
i.putExtra("aspectY", 1);
i.putExtra("outputX", 500);
i.putExtra("outputY", 500);
i.putExtra("return-data", true);
this.startActivityForResult(i, 7);
}
if(requestCode==7)
{
Bitmap bb=data.getParcelableExtra("data");
i.setImageBitmap(bb);
}
}
}
通过返回的请求值,我就知道是从哪个Activity返回来的,如果是从相机程序返回,那就获得拍照之后的数据,跳到图片处理程序。如果是从图片处理程序回来的,就获得返回的数据,显示处理之后的图片。
最后不要忘了在程序退出的时候删除在SDcard上没用的图片。
- @Override
- protected void onDestroy() {
- super.onDestroy();
- File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- if(f.exists())
- {
- f.delete();
- }
- }
@Override
protected void onDestroy() {
super.onDestroy();
File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
if(f.exists())
{
f.delete();
}
}
好了,就这样。
下面贴出运行效果图:
下面是本Activity的全部代码:
- package com.zhycheng.takephoto;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- 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 MainActivity extends Activity implements OnClickListener {
- String localTempImgFileName;
- String localTempImgDir="TestCamera";
- Button b;
- ImageView i;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- b=(Button) findViewById(R.id.button1);
- b.setOnClickListener(this);
- i=(ImageView) findViewById(R.id.imageView1);
- }
- @Override
- public void onClick(View v) {
- //doTakePhoto();
- File fos=null;
- try {
- fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- } catch (Exception e) {
- e.printStackTrace();
- }
- Uri u=Uri.fromFile(fos);
- Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
- i.putExtra(MediaStore.EXTRA_OUTPUT, u);
- this.startActivityForResult(i, 9);
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(resultCode==RESULT_OK)
- {
- if(requestCode==9)
- {
- File bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- Intent i=new Intent("com.android.camera.action.CROP");
- i.setType("image/*");
- //i.putExtra("data", bb);
- i.setDataAndType(Uri.fromFile(bb), "image/jpeg");
- i.putExtra("crop", "true");
- i.putExtra("aspectX", 1);
- i.putExtra("aspectY", 1);
- i.putExtra("outputX", 500);
- i.putExtra("outputY", 500);
- i.putExtra("return-data", true);
- this.startActivityForResult(i, 7);
- }
- if(requestCode==7)
- {
- Bitmap bb=data.getParcelableExtra("data");
- i.setImageBitmap(bb);
- }
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
- if(f.exists())
- {
- f.delete();
- }
- }
- }
package com.zhycheng.takephoto;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 MainActivity extends Activity implements OnClickListener {
String localTempImgFileName;
String localTempImgDir="TestCamera";
Button b;
ImageView i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button) findViewById(R.id.button1);
b.setOnClickListener(this);
i=(ImageView) findViewById(R.id.imageView1);
}
@Override
public void onClick(View v) {
//doTakePhoto();
File fos=null;
try {
fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
} catch (Exception e) {
e.printStackTrace();
}
Uri u=Uri.fromFile(fos);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
i.putExtra(MediaStore.EXTRA_OUTPUT, u);
this.startActivityForResult(i, 9);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK)
{
if(requestCode==9)
{
File bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
Intent i=new Intent("com.android.camera.action.CROP");
i.setType("image/*");
//i.putExtra("data", bb);
i.setDataAndType(Uri.fromFile(bb), "image/jpeg");
i.putExtra("crop", "true");
i.putExtra("aspectX", 1);
i.putExtra("aspectY", 1);
i.putExtra("outputX", 500);
i.putExtra("outputY", 500);
i.putExtra("return-data", true);
this.startActivityForResult(i, 7);
}
if(requestCode==7)
{
Bitmap bb=data.getParcelableExtra("data");
i.setImageBitmap(bb);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
if(f.exists())
{
f.delete();
}
}
}
工程代码下载: TakePhoto.zip