1

我正在尝试使用一种方法来裁剪图像。正是这种方法:

private void performCrop() {
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(**HERE**, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, 1234);
    }
    catch(ActivityNotFoundException anfe){
        Log.d("debugging","EXCEPTION");/*
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();*/
    }
}

这一行要求提供 Uri 数据和字符串类型。

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

但我只想把这个位图

Bitmap fotoCreada;

作为数据,但我不知道该怎么做。

有什么办法可以做到这一点?

谢谢。

编辑

好的,这是我现在的代码:

有一个按钮。当你点击它时:

public void onClick(View v) {
            Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
            if(isSDPresent) {
                //Creem carpeta
                File folder = new File(Environment.getExternalStorageDirectory().toString()+"/pic/");
                folder.mkdirs();
                //Recuperem data actual
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                String currentDateandTime = sdf.format(new Date());
                //Cridem a la camara.
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                resultingFile=new File(folder.toString() + "/"+currentDateandTime+".jpg");
                Uri uriSavedImage=Uri.fromFile(resultingFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                //Començem activity
                startActivityForResult(cameraIntent, 1888); 
            } else {
                Toast toast = Toast.makeText(getActivity(), "Issues while accessing sdcard.", Toast.LENGTH_SHORT);
                toast.show();
            }
        }   

如您所见,它正在调用 startActivityForResult。基本上拍了一张照片,我在activityResult上检索它:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("debugging",""+resultCode);
    if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
        Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        Bitmap photoRotated = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
        this.fotoCreada=photoRotated;
        ((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(this.fotoCreada);
        try {
            FileOutputStream out = new FileOutputStream(this.resultingFile.getPath());
            this.fotoCreada.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        performCrop();
    }
    if (requestCode == 1234 && resultCode == -1){
        Bitmap bitmap = (Bitmap) data.getParcelableExtra("BitmapImage");
        Log.d("debugging",""+bitmap.getHeight());
    }
}

从前面的代码可以看出,当我检索位图时,我调用方法 performCrop。现在的代码是:

private void performCrop() {
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        //cropIntent.setDataAndType(this.picUri, "image/*");
        cropIntent.putExtra("BitmapImage", this.fotoCreada);
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, 1234);
        Log.d("debugging","he acabat performCrop");
    }
    catch(ActivityNotFoundException anfe){
        Log.d("debugging","EXCEPTION");
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

但是 performCrop 中的 startActivityForResult 永远不会调用 onActivityResult。日志猫说它只输入了一次,应该输入两次。第一个来自相机活动,第二个来自作物活动。

-1
he acabat performCrop

希望它足够清楚。

谢谢。

4

2 回答 2

2

Bitmapimplements Parcelable,所以你总是可以在意图中传递它:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

并在另一端检索它:

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

但是,如果位图作为文件或资源存在,最好传递位图的 URI 或 ResourceID,而不是位图本身。传递整个位图需要大量内存。传递 URL 需要很少的内存,并允许每个活动根据需要加载和缩放位图。

于 2013-01-20T20:37:49.820 回答
0

使用 Intent.putExtra。位图是可解析的,所以它会起作用。setDataAndType 用于 URI,但您没有引用 Web 或磁盘上的资源。

于 2013-01-20T20:37:14.780 回答