0
    public void onClick(View v) {
            // TODO Auto-generated method stub

                    if (v.getId() == R.id.textView7)
                 {     
                        try {

                            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                            startActivityForResult(captureIntent, CAMERA_CAPTURE);
                        }
                        catch(ActivityNotFoundException anfe)
                {

                            String errorMessage = "Whoops - your device doesn't support capturing images!";
                            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    }
                }protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                    if (resultCode == RESULT_OK) {

                        if(requestCode == CAMERA_CAPTURE){

                            picUri = data.getData();
                                                            performCrop();
                        }

                        else if(requestCode == PIC_CROP){

                            Bundle extras = data.getExtras();

                            Bitmap thePic = extras.getParcelable("data");

                            ImageView picView = (ImageView)findViewById(R.id.imageView1);

                            picView.setImageBitmap(thePic);
                        }
                        else if (requestCode == SELECT_PICTURE) {
                                Uri selectedImageUri = data.getData();
                                selectedImagePath = getPath(selectedImageUri);
                                System.out.println("Image Path : " + selectedImagePath);
                                iv1.setImageURI(selectedImageUri);
                            }
                    }
                }


                private void performCrop(){

                    try {

                        Intent cropIntent = new Intent("com.android.camera.action.CROP"); 

                        cropIntent.setDataAndType(picUri, "image/*");

                        cropIntent.putExtra("crop", "true");

                        cropIntent.putExtra("aspectX", 1);
                        cropIntent.putExtra("aspectY", 1);

                        cropIntent.putExtra("outputX", 256);
                        cropIntent.putExtra("outputY", 256);

                        cropIntent.putExtra("return-data", true);

                        startActivityForResult(cropIntent, PIC_CROP);  
                    }

                    catch(ActivityNotFoundException anfe){

                        Toast toast = Toast.makeText(this, "Mobile doesnot support this feature",Toast.LENGTH_SHORT);
                        toast.show();
                    }
        }

                public String getPath(Uri uri) {
                    String[] projection = { MediaStore.Images.Media.DATA };
                    @SuppressWarnings("deprecation")
                    Cursor cursor = managedQuery(uri, projection, null, null, null);
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    return cursor.getString(column_index);
                }

ボタンをクリックしてこれを使用して画像をキャプチャしようとしましたが、クリックしても何も表示されません画像をキャプチャするボタンを作成しましたが、エラーが表示されます!! 助けてください

私は初心者で、学びたいです

4

2 に答える 2

0

マニフェストに以下の 2 行を追加します。

 <uses-permission android:name="android.permission.CAMERA" />
  <uses-feature android:name="android.hardware.camera" />
于 2013-09-04T11:30:38.820 に答える
0

これは私と一緒に魅力のように機能しています

private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;

public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }


    public String getImagePath() {
        return imgPath;
    }

btnGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
于 2013-09-04T11:33:11.377 に答える