zoukankan      html  css  js  c++  java
  • Android打开相机和打开相册

    打开相机

     /**
         * 选择相机
         */
    
        private void showCamera() {
            // 跳转到系统照相机
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                // 设置系统相机拍照后的输出路径
                // 创建临时文件
                mTmpFile = OtherUtils.createFile(getApplicationContext());
    
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
                startActivityForResult(cameraIntent, REQUEST_CAMERA);
            } else {
                Toast.makeText(getApplicationContext(), R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
            }
    
        }

    打开相册

      Intent albumIntent = new Intent(Intent.ACTION_PICK, null);
            albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(albumIntent, REQUEST_ALBUM_OK);

    onActivityResult

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            switch (requestCode) {
                case REQUEST_CAMERA:
                    //请求相机
                    if (mTmpFile != null) {
                        Log.d(TAG, "onActivityResult: 请求相机: " + mTmpFile.getAbsolutePath());
    
                        Picasso.with(this).load(mTmpFile).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100))
                                .error(R.mipmap.pictures_no).into(mIvDispaly);
                    }
                    break;
                case REQUEST_ALBUM_OK:
                    Log.d(TAG, "onActivityResult:相册 " + data.getData().toString());
                    ContentResolver resolver = getContentResolver();
    
                    try {
                        InputStream inputStream = resolver.openInputStream(data.getData());
    
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    
                        mIvDispaly.setImageBitmap(bitmap);
    
                    } catch (FileNotFoundException e) {
    
    
                        e.printStackTrace();
                    }
    
                    //                Cursor query = resolver.query(data.getData(), null, null, null, null);
    //
    //                String str = null;
    //                while (query.moveToNext()) {
    //                    Log.d(TAG, "onActivityResult:数量 " + query.getCount());
    //                    str =query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
    //                    Log.d(TAG, "onActivityResult: 列名" + query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA)));
    //                }
    //                query.close();
    //                Picasso.with(this).load(new File(str)).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100))
    //                        .error(R.mipmap.pictures_no).into(mIvDispaly);
                    break;
    
            }
        }

    OtherUtils

    import android.content.Context;
    import android.os.Environment;
    
    import java.io.File;
    import java.util.Date;
    
    /**
     * Created by 刘楠 on 2016/7/20 0020.22:37
     */
    public class OtherUtils {
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        /**
         * 获取拍照相片存储文件
         * @param context
         * @return
         */
        public static File createFile(Context context){
            File file;
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                String timeStamp = String.valueOf(new Date().getTime());
                file = new File(Environment.getExternalStorageDirectory() +
                        File.separator + timeStamp+".jpg");
            }else{
                File cacheDir = context.getCacheDir();
                String timeStamp = String.valueOf(new Date().getTime());
                file = new File(cacheDir, timeStamp+".jpg");
            }
            return file;
        }
    
    }
  • 相关阅读:
    SrsDataConnector The SQL Server Reporting Services account is a local user and is not supported.
    Marketing with Microsoft Dynamics CRM IDEA CONFERENCE
    Discuz!X 系列 HTTP_X_FORWARDED_FOR 绕过限制进行密码爆破
    PHPCMS v9.5.6 通杀getshell(前台)
    PHPCMS v9.5.8-设计缺陷可重置前台任意用户密码
    PHPCMS v9.6.0 任意用户密码重置
    PHPCMS V9 任意文件下载(Windows)
    Joomla!3.7.0 Core SQL注入漏洞动态调试草稿
    利用FOFA搜索WatchGuard防火墙
    如何删除Windows休眠文件(hiberfil.sys)
  • 原文地址:https://www.cnblogs.com/liunanjava/p/5768147.html
Copyright © 2011-2022 走看看