csh_34 2013-03-19 07:39 采纳率: 0%
浏览 10447
已采纳

使用照相机拍照,获取图片路径

在程序中我使用手机相机拍一张照片,然后把这张图片保存在手机图库里面。然后我想获取图片的路径,再保存到数据库中。如何实现呢?

public void onClick(View v){
        switch(v.getId())
        {
            case R.id.btnLoadPic:

                //Options for the dialogue menu
                final CharSequence[] items = {"Camera", "Gallery"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Choose an Option");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    /**
                     * Make onclick functionality for the options in the dialogue menu
                     */
                    public void onClick(DialogInterface dialog, int item) {

                        // Camera option
                        if (item == 0){

                            PackageManager pm = getPackageManager();

                            if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                                //Toast.makeText(this, "camera", Toast.LENGTH_SHORT).show();
                                dispatchTakePictureIntent(11);
                            } else {
                                Toast.makeText(null, "No camera avalible", Toast.LENGTH_SHORT).show();

                            }
                        }
                        // Gallery option this works fine


    private void dispatchTakePictureIntent(int actionCode) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, actionCode);


            //handleSmallCameraPhoto(takePictureIntent);
        }

        private void handleSmallCameraPhoto(Intent intent) {
            Bundle extras = intent.getExtras();
            Bitmap mImageBitmap = (Bitmap) extras.get("data");
            ImageView mImageView = (ImageView) this.findViewById(R.id.imagePlayer);
            mImageView.setImageBitmap(mImageBitmap);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            handleSmallCameraPhoto(data);
        }

后面的代码访问照相机,然后在 imageview 中显示图像,如何在一个字符串格式中获取图片的路径?

  • 写回答

1条回答 默认 最新

  • yongyong_21 2013-03-19 08:46
    关注

    测试使用下面的代码:

              @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                    startActivityForResult(intent, REQUEST_IMAGE);
                }
            });
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
                try {
                    FileInputStream in = new FileInputStream(destination);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 10;
                    imagePath = destination.getAbsolutePath();
                    tvPath.setText(imagePath);
                    Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
                    picture.setImageBitmap(bmp);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            else{
                tvPath.setText("Request cancelled");
            }
        }
    
        public String dateToString(Date date, String format) {
            SimpleDateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        }
    }
    

    不要忘记在 manifest 文件中添加权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致