程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> camera-使用照相機拍照,獲取圖片路徑

camera-使用照相機拍照,獲取圖片路徑

編輯:編程綜合問答
使用照相機拍照,獲取圖片路徑

在程序中我使用手機相機拍一張照片,然後把這張圖片保存在手機圖庫裡面。然後我想獲取圖片的路徑,再保存到數據庫中。如何實現呢?

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 中顯示圖像,如何在一個字符串格式中獲取圖片的路徑?

最佳回答:


測試使用下面的代碼:

          @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"/>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved