Android调用相机拍照高清原图(兼容7.0)

在安卓更新7.0的版本后,要调用相机拍照获取原图则需要先把拍摄后的内容保存到目录,然后再借助provider调出来显示,相比以前可以说十分繁琐,但为了摆脱马赛克画质的困扰,为了更好的用户体验,还是硬着头皮上吧



亮丽的苹果君:

在这里插入图片描述

在你的main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="take"
        android:id="@+id/btntake"/>


    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/image"

        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content" />
</RelativeLayout>



在Mainfest.xml的application内定义provider:

  <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    
    
    <provider
        android:authorities="comgin.example.root.hightqualitycame.fileprovider"
这个自定义,但引用时不能错
        android:name="android.support.v4.content.FileProvider"
这个照抄官网
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS"这个不能乱动

            android:resource="@xml/file_paths"></meta-data>
这个写能下创建xml文件的位置

    </provider>
</application>

还是mainfest加入要用的权限

<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>



右键res新建一个名叫xml的目录,在目录下创建file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">


    <external-path
        name="codecity_image"
名字随意
        path="Pictures"></external-path>
这里要写图片存在的目录,不然会卡,而且不要全路径
</paths>



预备工作搞定最后写我们的MainActivity.java:


public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;


  File  showPIC;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button=findViewById(R.id.btntake);
        imageView=findViewById(R.id.image);


if (Build.VERSION.SDK_INT>=23){
    requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
}//请求权限












        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dispafuckpic();

            }
        });
    }










    private void dispafuckpic() {
        Intent takepic=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takepic.resolveActivity(getPackageManager())!=null){
            //这代表你的app可以允许相机


            File photofile=null;




            try{


                photofile=cratephotofile();//去创建文件方法,获得图片路径创建的
                showPIC=photofile;

if (photofile!=null) {
    String pathtofile = photofile.getAbsolutePath();
Log.e("testt","seee:"+pathtofile);
    Uri photouri=FileProvider.getUriForFile(MainActivity.this,"comgin.example.root.hightqualitycame.fileprovider",photofile);
    //把这个图片和其他app分享,AUTHORITY是自己定义的,第三个必须是完整的图片路径
    takepic.putExtra(MediaStore.EXTRA_OUTPUT,photouri);

//把share的Uri放进选好的路径


    startActivityForResult(takepic,1);
}

            }
 catch (IOException e) {
                e.printStackTrace();
            }

        }
    }















    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==1){
                Bitmap bitmap=BitmapFactory.decodeFile(String.valueOf(showPIC));
//String.valueOf进行一个String转型
                

                imageView.setImageBitmap(bitmap);
            }
        }
    }








    public File  cratephotofile() throws IOException {//返回一个File类的文件



String name=new SimpleDateFormat("YYYYMMdd_HHmmss").format(new Date());
//年月日小时分秒


      File stordir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      //获得公共目录下的图片文件路径


      File image=File.createTempFile(name,".jpg",stordir);
      //1:字首2:后缀3:在哪个目录下

return  image;
    }

}

代码分析:
点击button调用dispafuckpic()方法,这个方法会执行调用相机和创建相机文件方法 cratephotofile(),它会把你拍照获得的相片保存在相应的Picture目录内,它返回的image赋值给全局变量showPIC,这个showPIC就是你的照片原图,最后转型成bitmap显示,大功告成。




坑:

Uri photouri=FileProvider.getUriForFile(MainActivity.this,"comgin.example.root.hightqualitycame.fileprovider",photofile);

comgin.example.root.hightqualitycame.fileprovider一定要和你在Mainfest定义的authorities属性一毛一样,要不然运行马上卡掉还不带提示(博主应为曾经因为这个bug浪费了一天)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值