美文网首页
Android12-调用摄像头和相册

Android12-调用摄像头和相册

作者: figure_ai | 来源:发表于2017-04-13 16:27 被阅读0次
    • 运行结果如下:
    2017-04-13_16-20-51.gif

    第一步:搭建界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
    
        <Button
            android:id="@+id/take_photo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="拍照上传"/>
    
        <Button
            android:id="@+id/choose_from_album"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="从相册中选择照片"/>
    
        <ImageView
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
    

    第二步:在Activity中实现功能

    public class MainActivity extends AppCompatActivity {
    
    
        public static final int TAKE_PHOTO = 1;
    
        private ImageView picture;
    
        private Uri imageUri;
    
        public static final int CHOOSE_PHOTO = 2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_main);
    
            Button takePhoto = (Button) findViewById(R.id.take_photo);
            picture = (ImageView) findViewById(R.id.picture);
    
            takePhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //创建file对象,用于储存拍照后的图片,getExternalCacheDir() : 将照片存放在手机的关联缓存目录下
                    File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
                    try {
                        if (outputImage.exists()) {
                            outputImage.delete();
                        }
                        outputImage.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (Build.VERSION.SDK_INT >= 24) {
                        //如果系统版本大于7.0,调用FileProvider的getUriForFile()方法将File对象转换成一个封装过的Uri对象
                        //getUriForFile()参数说明
                        //第一个参数:Context对象
                        //第二个参数:任意唯一的字符串
                        //第三个参数:创建的File对象
                        imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", outputImage);
                    } else {
                        //否则,调用Uri的fromFile()方法将File对象转换成Uri对象
                        imageUri = Uri.fromFile(outputImage);
                    }
                    //启动相机程序
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    //设定图片的输出地址
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    //使用startActivityForResult()启动Intent时,Intent结束后会有结果返回到onActivityResult()方法中。
                    startActivityForResult(intent, TAKE_PHOTO);
                }
            });
    
            //从相册中选择照片逻辑
            final Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
            chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //如果未授权
                    if (ContextCompat.checkSelfPermission(MainActivity.this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        //发起授权询问
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{ WRITE_EXTERNAL_STORAGE }, 1);
                    } else {
                        //如果已授权,直接打开相册
                        openAlbum();
                    }
                }
            });
        }
        ///打开相册的方法
        private void openAlbum() {
            Intent intent = new Intent("android.intent.action.GET_CONTENT");
            intent.setType("image/*");
            startActivityForResult(intent, CHOOSE_PHOTO);
        }
        ///用户选择完授权询问后调用
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case 1:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        //如果用户选择授权,打开相册
                        openAlbum();
                    } else {
                        //如果用户拒绝,弹出提示框
                        Toast.makeText(this, "获取相册失败,你拒绝了授权请求", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
        }
    
        //Activity跳转之后再返回时调用
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case TAKE_PHOTO:
                    if (resultCode == RESULT_OK) {
                        //如果拍照成功
                        try {
                            //将拍摄的照片显示出来
                            Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                            picture.setImageBitmap(bitmap);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
    
                case CHOOSE_PHOTO:
                    if (resultCode == RESULT_OK) {
                        //判断手机系统版本号
                        if (Build.VERSION.SDK_INT >= 19) {
                            //4.4及以上系统使用这个方法处理图片
                            handleImageOnKitKat(data);
                        } else {
                            //4.4及以下系统使用这个方法处理图片
                            handleImageBeforeKitkat(data);
                        }
                    }
               default:
                    break;
            }
        }
    
        @TargetApi(19)
        private void handleImageOnKitKat(Intent data) {
            String imagePath = null;
            Uri uri = data.getData();
            if (DocumentsContract.isDocumentUri(this, uri)) {
                //如果是document类型的Uri,则通过document id处理
                String docId = DocumentsContract.getDocumentId(uri);
                if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                    String id = docId.split(":")[1];    //解析出数字格式的id
                    String selection = MediaStore.Images.Media._ID + "=" + id;
                    imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
                } else if ("com.android.provides.downloads.documents".equals(uri.getAuthority())) {
                    Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                    imagePath = getImagePath(contentUri, null);
                }
            } else if ("content".equalsIgnoreCase(uri.getScheme())) {
                //如果是content类型的Uri,则使用普通方式处理
                imagePath = getImagePath(uri, null);
            } else if ("file".equalsIgnoreCase(uri.getScheme())) {
                //如果是file类型的Uri,直接获取图片的路径即可
                imagePath = uri.getPath();
            }
            //根据图片路径显示图片
            displayImage(imagePath);
        }
    
        private void handleImageBeforeKitkat(Intent data) {
            Uri uri = data.getData();
            String imagePath = getImagePath(uri, null);
            displayImage(imagePath);
        }
    
        private String getImagePath(Uri uri, String selection) {
            String path = null;
            //通过Uri和selection来获取真实的图片路径
            Cursor cursor = getContentResolver().query(uri, null, selection,null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                }
                cursor.close();
            }
            return path;
        }
        //设置图片
        private void displayImage(String imagePath) {
            if (imagePath != null) {
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                picture.setImageBitmap(bitmap);
            } else {
                Toast.makeText(this,"获取图片失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
    

    第三步:在Manifest添加权限声明

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.anwser_mac.cameraalbumtest">
    
        <!--声明读取sdcard的权限-->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <!--注册内容提供器-->
            <provider
                android:authorities="com.example.cameraalbumtest.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"/>
            </provider>
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    相关文章

      网友评论

          本文标题:Android12-调用摄像头和相册

          本文链接:https://www.haomeiwen.com/subject/ibgbattx.html