Camera Activity不返回图像路径

时间:2015-07-06 06:31:28

标签: android

我的任务是从相机捕获图像并将该图像发送到裁剪Intent。以下是我写的代码

进行相机拍摄

Intent captureIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);

在活动结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_CAPTURE) {
            // get the Uri for the captured image
            picUri = data.getData();  // picUri is global string variable 
            performCrop();
        }
   }

}

public void performCrop() {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");

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

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

        cropIntent.putExtra("aspectX", 3);
        cropIntent.putExtra("aspectY", 2);
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);

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

        startActivityForResult(cropIntent, CROP_PIC);
    } catch (ActivityNotFoundException anfe) {

        String errorMessage = "Your device doesn't support the crop action";
        Toast toast = Toast.makeText(getApplicationContext(), errorMessage,
                Toast.LENGTH_SHORT);
        toast.show();
    }
}

我在不同设备上获得不同的行为

在某些设备中,我收到错误"无法找到项目"。 在捕获图像活动后的某些设备中,并且没有继续

我也试过this

请告诉我正确的方法

3 个答案:

答案 0 :(得分:1)

//在课堂上声明

private static int RESULT_LOAD_IMAGE = 1;

String picturePath="";

//在按钮点击事件中写入

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

//在onCreate()

之后复制此代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.img); //place imageview in your xml file
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

在清单文件中写下以下权限

1.read外部存储

2.写外部存储空间

答案 1 :(得分:1)

您可以通过以下方式调用意图:

int REQUEST_IMAGE_CAPTURE = 1;
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                ((Activity) context).startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

在OnActivityResult内的活动中,你会得到这样的路径:

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE) {

                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                    Matrix matrix = new Matrix();

                    matrix.postRotate(-90);

                    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    byte[] attachmentBytes = byteArrayOutputStream.toByteArray();

                    String attachmentData = Base64.encodeToString(attachmentBytes, Base64.DEFAULT);


                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "CTSTemp" + File.separator + "default";
                    f.delete();
                    ESLogging.debug("Bytes size = " + attachmentBytes.length);
                    ESLogging.debug("FilePath = " + path);
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        ESLogging.error("FileNotFoundException while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    } catch (IOException e) {
                        ESLogging.error("IOException while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    } catch (Exception e) {
                        ESLogging.error("Exception while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    ESLogging.error("Exception while uploading new attachment in class HomeActivity", e);
                    e.printStackTrace();
                }
            } 
}
    }

答案 2 :(得分:0)