最近看了几篇关于Android照相机的一些文章,如今总结以下,直接上源代码把,该说的都用注释说完了java
package org.android.test; 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; import android.widget.Toast; public class Android_mytestActivity extends Activity { /** Called when the activity is first created. */ // 定义一个button打开照相机,定义一个imageview显示照相机所拍摄的相片; Button but,upload_image; ImageView img; // 获取sd卡根目录地址,并建立图片父目录文件对象和文件的对象; String file_str = Environment.getExternalStorageDirectory().getPath(); File mars_file = new File(file_str + "/my_camera"); File file_go = new File(file_str + "/my_camera/file.jpg"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but = (Button) findViewById(R.id.my_camare_button); upload_image=(Button)findViewById(R.id.upload_image); img = (ImageView) findViewById(R.id.my_img_view); //拍照 but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 验证sd卡是否正确安装: if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { // 先建立父目录,若是新建立一个文件的时候,父目录没有存在,那么必须先建立父目录,再新建文件。 if (!mars_file.exists()) { mars_file.mkdirs(); } /*//常规状况下,咱们这里会 建立子目录,但在这里不用系统拍照完毕后会根据所给的图片路径自动去实现; if(!file_go.exists()) { try { file_go.createNewFile(); } catch (IOException e) { }} */ // 设置跳转的系统拍照的activity为:MediaStore.ACTION_IMAGE_CAPTURE ; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 并设置拍照的存在方式为外部存储和存储的路径; intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file_go)); //跳转到拍照界面; startActivityForResult(intent, 0x1); } else { Toast.makeText(Android_mytestActivity.this, "请先安装好sd卡", Toast.LENGTH_LONG).show(); } } }); //上传 upload_image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(file_go.exists()) { //验证图片存在后就实现,上传功能,获得与服务器的输出流... //什么URLconnection ,HttpURLconnectio等均可以....... Toast.makeText(Android_mytestActivity.this, "上传中....", Toast.LENGTH_LONG).show(); } else { Toast.makeText(Android_mytestActivity.this, "请先拍照....", Toast.LENGTH_LONG).show(); } } }); } //拍照结束后显示图片; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // 判断请求码和结果码是否正确,若是正确的话就在activity上显示刚刚所拍照的图片; if (requestCode == 0x1 && resultCode == this.RESULT_OK) { /* 使用BitmapFactory.Options类防止OOM(Out Of Memory)的问题; 建立一个BitmapFactory.Options类用来处理bitmap;*/ BitmapFactory.Options myoptions=new BitmapFactory.Options(); /* 设置Options对象inJustDecodeBounds的属性为true,用于在BitmapFactory的 decodeFile(String path, Options opt)后获取图片的高和宽; 并且设置了他的属性值为true后使用BitmapFactory的decodeFile()方法没法返回一张 图片的bitmap对象,仅仅是把图片的高和宽信息给Options对象; */ myoptions.inJustDecodeBounds=true; BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions); //根据在图片的宽和高,获得图片在不变形的状况指定大小下的缩略图,设置宽为222; int height=myoptions.outHeight*222/myoptions.outWidth; myoptions.outWidth=222; myoptions.outHeight=height; //在从新设置玩图片显示的高和宽后记住要修改,Options对象inJustDecodeBounds的属性为false; //否则没法显示图片; myoptions.inJustDecodeBounds=false; //还没完这里才刚开始,要节约内存还须要几个属性,下面是最关键的一个; myoptions.inSampleSize=myoptions.outWidth/222; //还能够设置其余几个属性用于缩小内存; myoptions.inPurgeable=true; myoptions.inInputShareable=true; myoptions.inPreferredConfig=Bitmap.Config.ARGB_4444;// 默认是Bitmap.Config.ARGB_8888 //成功了,下面就显示图片咯; Bitmap bitmat = BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions); img.setImageBitmap(bitmat); } else { System.out.println("不显示图片"); } super.onActivityResult(requestCode, resultCode, data); } }
2.布局文件linux
<?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:background="#aaaaaa" android:orientation="vertical" > <Button android:id="@+id/my_camare_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击拍照" /> <Button android:id="@+id/upload_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="上传相片" /> <ImageView android:id="@+id/my_img_view" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
3.主配置文件android
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:Android="http://schemas.android.com/apk/res/android" package="org.android.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <!-- <!– 对SD卡进行写权限 –> --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 提供建立与删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Android_mytestActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 注意必定要加关于文件读写的权限,有个权限我没有加(就是照相机的): <uses-permission android:name="android.permission.CAMERA" /> 由于咱们是跳转的系统的照相机的activity,因此关于拍照仍然是系统完成的,所以这里不须要加上这个权限。