实现此功能思路:调用相机拍照,然后把照片存储在固定路径,在去读取路径的照片。
先配置权限,在AndroidManifest.xml中配置读写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
1.打开相机的代码
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
imageUri=Uri.fromFile(new File(path));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
其中path为拍照后的存储路径,1为拍照后的返回值。
2.拍照后返回结果,核心代码
Bitmap bitmap= BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
这里用Bitmap来传递图片,可能会出现图片太大无法传输的情况,在AndroidManifest.xml的application中加上
android:hardwareAccelerated="false"
3.在安卓7.0上加上这段代码可正常运行
//android 7.0系统解决拍照的问题
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
动态申请权限
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,//联系人的权限
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};//读写SD卡权限
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
}
}
}
以下为完整代码
MainActivity
public class MainActivity extends AppCompatActivity {
Uri imageUri;
Button btn;
ImageView imageView;
String path= Environment.getExternalStorageDirectory()+File.separator+"DCIM"+File.separator+"Camera"+File.separator+"temp.jpg";
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.iv_selected);
btn=findViewById(R.id.btn_camera);
Permission();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openCarema();
}
});
}
/**
* 打开相机
*/
private void openCarema() {
//启动相机程序
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
imageUri=Uri.fromFile(new File(path));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
try{
Bitmap bitmap= BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
}catch (Exception e){e.printStackTrace();}
break;
}
}
public void Permission(){
//安卓7.0调用相机
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
//动态申请权限
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,//联系人的权限
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};//读写SD卡权限
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
}
}
}
}
}
Xml文件:
<RelativeLayout xmlns: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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/bottombar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="@+id/btn_camera"
android:layout_weight="4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="拍照" />
</LinearLayout>
<RelativeLayout
android:id="@+id/bottombar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottombar1"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:orientation="horizontal" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottombar"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选取的图片:" />
<ImageView
android:id="@+id/iv_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="300dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>