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"/>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?