Android调用摄像头拍照并存储
吃橘子的季节呢 人气:01、前期准备
需要在Manifest中添加相关权限
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、主要方法
1、需要使用Intent调用摄像头
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用Camera startActivityForResult(intent, Activity.RESULT_OK);//如果正常,则返回 Activity.RESULT_OK 本质为 int类型的1
2、需要检查SD卡(外部存储)状态
Environment.MEDIA_MOUNTED
sd卡在手机上正常使用状态
Environment.MEDIA_UNMOUNTED
用户手工到手机设置中卸载sd卡之后的状态
Environment.MEDIA_REMOVED
用户手动卸载,然后将sd卡从手机取出之后的状态
Environment.MEDIA_BAD_REMOVAL
用户未到手机设置中手动卸载sd卡,直接拨出之后的状态
Environment.MEDIA_SHARED
手机直接连接到电脑作为u盘使用之后的状态
Environment.MEDIA_CHECKINGS
手机正在扫描sd卡过程中的状态
在代码中的判断
String sdStatus = Environment.getExternalStorageState(); if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){ System.out.println(" ------------- sd card is not avaiable ---------------"); return; }
3、获取图片及其压缩图片
String name = "photo.jpg";//定义图片名称 Bundle bundle = data.getExtras();//data为onActivityResult中的intent类型的参数 Bitmap bitmap = (Bitmap) bundle.get("data");//bitmap // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"); // file.mkdirs(); //创建文件夹 // String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name; String fileName = "sdcard"+"/"+name;//初始化文件路径 FileOutputStream fos =null;//初始化文件输出流 try { // System.out.println(fileName); // 测试用,查看文件路径 fos=new FileOutputStream(fileName); // 输出文件到外部存储 //今天第一次正视这个bitmap.compress()方法,它用来压缩图片大小。 /* 这个方法有三个参数: Bitmap.CompressFormat format 图像的压缩格式; int quality 图像压缩率,0-100。 0 压缩100%,100意味着不压缩; OutputStream stream 写入压缩数据的输出流; public boolean compress(CompressFormat format, int quality, OutputStream stream) {} 返回值boolean类型 如果成功地把压缩数据写入输出流,则返回true。 */ bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fos.flush(); //释放输出流 fos.close(); } catch (IOException e) { e.printStackTrace(); } }
3、案例展示
1、Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_take_photo" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="take photo" android:id="@+id/takephoto" /> <ImageView android:layout_below="@+id/takephoto" android:layout_width="400dp" android:layout_height="400dp" android:id="@+id/picture" /> </RelativeLayout>
2、MainActivity
package icu.whatsblog.CameraCrop; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Environment; import android.provider.MediaStore; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import lee.suk.cameracrop.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((Button) findViewById(R.id.takephoto)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK){ String sdStatus = Environment.getExternalStorageState(); if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){ System.out.println(" ------------- sd card is not avaiable ---------------"); return; } String name = "photo.jpg"; Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data"); // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"); // file.mkdirs(); //创建文件夹 // String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name; String fileName = "sdcard"+"/"+name; FileOutputStream fos =null; try { System.out.println(fileName); fos=new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } ((ImageView) findViewById(R.id.picture)).setImageBitmap(bitmap); } } }
加载全部内容