Android 调用系统相机拍摄获取照片的两种方法实现实例
Android调用系统相机拍摄获取照片的两种方法实现实例
在我们Android开发中经常需要做这个一个功能,调用系统相机拍照,然后获取拍摄的照片。下面是我总结的两种方法获取拍摄之后的照片,一种是通过Bundle来获取压缩过的照片,一种是通过SD卡获取的原图。
下面是演示代码:
布局文件:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照获取缩略图"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:text="拍照获取原图"/> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher"/> </LinearLayout>
java代码:
packagecom.example.cameardemo; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importandroid.content.Intent; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.net.Uri; importandroid.os.Bundle; importandroid.os.Environment; importandroid.provider.MediaStore; importandroid.support.v7.app.ActionBarActivity; importandroid.util.Log; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.ImageView; publicclassMainActivityextendsActionBarActivityimplementsOnClickListener{ privatestaticintREQUEST_THUMBNAIL=1;//请求缩略图信号标识 privatestaticintREQUEST_ORIGINAL=2;//请求原图信号标识 privateButtonbutton1,button2; privateImageViewmImageView; privateStringsdPath;//SD卡的路径 privateStringpicPath;//图片存储路径 @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); button1=(Button)findViewById(R.id.button1); button2=(Button)findViewById(R.id.button2); mImageView=(ImageView)findViewById(R.id.imageView1); button1.setOnClickListener(this); button2.setOnClickListener(this); //获取SD卡的路径 sdPath=Environment.getExternalStorageDirectory().getPath(); picPath=sdPath+"/"+"temp.png"; Log.e("sdPath1",sdPath); } @Override publicvoidonClick(Viewview){ switch(view.getId()){ caseR.id.button1://第一种方法,获取压缩图 Intentintent1=newIntent(MediaStore.ACTION_IMAGE_CAPTURE); //启动相机 startActivityForResult(intent1,REQUEST_THUMBNAIL); break; caseR.id.button2://第二种方法,获取原图 Intentintent2=newIntent(MediaStore.ACTION_IMAGE_CAPTURE); Uriuri=Uri.fromFile(newFile(picPath)); //为拍摄的图片指定一个存储的路径 intent2.putExtra(MediaStore.EXTRA_OUTPUT,uri); startActivityForResult(intent2,REQUEST_ORIGINAL); break; default: break; } } /** *返回应用时回调方法 */ @Override protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ super.onActivityResult(requestCode,resultCode,data); if(resultCode==RESULT_OK){ if(requestCode==REQUEST_THUMBNAIL){//对应第一种方法 /** *通过这种方法取出的拍摄会默认压缩,因为如果相机的像素比较高拍摄出来的图会比较高清, *如果图太大会造成内存溢出(OOM),因此此种方法会默认给图片尽心压缩 */ Bundlebundle=data.getExtras(); Bitmapbitmap=(Bitmap)bundle.get("data"); mImageView.setImageBitmap(bitmap); } elseif(resultCode==REQUEST_ORIGINAL){//对应第二种方法 /** *这种方法是通过内存卡的路径进行读取图片,所以的到的图片是拍摄的原图 */ FileInputStreamfis=null; try{ Log.e("sdPath2",picPath); //把图片转化为字节流 fis=newFileInputStream(picPath); //把流转化图片 Bitmapbitmap=BitmapFactory.decodeStream(fis); mImageView.setImageBitmap(bitmap); }catch(FileNotFoundExceptione){ e.printStackTrace(); }finally{ try{ fis.close();//关闭流 }catch(IOExceptione){ e.printStackTrace(); } } } } } }
最后不要忘记在清单文件上添加上读取SD卡的权限:
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.example.cameardemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19"/> <!--操作sd卡的权限--> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name="com.example.cameardemo.MainActivity" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> <!--具有相机功能--> <intent-filter> <actionandroid:name="android.media.action.IMAGE_CAPTURE"/> <categoryandroid:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> </manifest>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!