【问题标题】:How to save cropped image in android instead of returning cropping data?如何在android中保存裁剪的图像而不是返回裁剪数据?
【发布时间】:2012-10-31 11:32:18
【问题描述】:

我有一个应用程序,我可以让用户选择画廊图像或用相机拍照,然后我让他选择裁剪它。

现在当用户裁剪图像时,Gallery 裁剪器返回裁剪部分的 ByteArray,是否可以传递一个参数要求 Gallery 裁剪器将输出保存到文件(就像我开始意图捕获和相机中的图像)

例如:

这是我使用的裁剪代码:

// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");

// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
    Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
    return;
} else {

// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image

    Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));

    // Send the Uri path to the cropper intent
    intent.setData(imageUri); 
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 200);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);         
    intent.putExtra("scale", true);
    // Here's my attempt to ask the intent to save output data as file
    File f = null;
    // Here I initialize empty file 
    try{
            // This returns the file created
        f = setUpCroppedFile();
        mCurrentThumbPath = f.getAbsolutePath();            
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));            
        intent.putExtra("output", Uri.fromFile(f));         
    }
    catch(IOException e){
        e.printStackTrace();
    }
    // --------------------------------------------------------------------
    // -----------> When changing this to false it worked <----------------
    // --------------------------------------------------------------------        
    intent.putExtra("return-data", true);
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------

    // If there's only 1 Cropper in the phone (e.g. Gallery )
    if (size == 1) {
            // get the cropper intent found
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROPPED_PHOTO);
    }

   // Please ignore the case if there are more . . I know how to handle it 

更新:问题已解决,我知道我会将其保留在此处,以防其他人发现它有用(请参阅上面代码中的大注释)

【问题讨论】:

    标签: android android-image


    【解决方案1】:

    此代码是修改后的代码,它可以工作:

    // Initialize intent
    Intent intent = new Intent("com.android.camera.action.CROP");
    // set data type to be sent
    intent.setType("image/*");
    
    // get croppers available in the phone
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
    int size = list.size();
    // handle the case if there's no cropper in the phone
    if (size == 0) {
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
        return;
    } else {
    
    // now this is the case if cropper exists
    // initialize the Uri for the captures or gallery image
    
        Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));
    
        // Send the Uri path to the cropper intent
        intent.setData(imageUri); 
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);         
        intent.putExtra("scale", true);
        // Here's my attempt to ask the intent to save output data as file
        File f = null;
        // Here I initialize empty file 
        try{
                // This returns the file created
            f = setUpCroppedFile();
            mCurrentThumbPath = f.getAbsolutePath();            
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));            
            intent.putExtra("output", Uri.fromFile(f));         
        }
        catch(IOException e){
            e.printStackTrace();
        }
        // --------------------------------------------------------------------
        // -----------> When changing this to false it worked <----------------
        // --------------------------------------------------------------------        
        intent.putExtra("return-data", false);
        // --------------------------------------------------------------------
        // --------------------------------------------------------------------
    
        // If there's only 1 Cropper in the phone (e.g. Gallery )
        if (size == 1) {
                // get the cropper intent found
                Intent i        = new Intent(intent);
                ResolveInfo res = list.get(0);
    
                i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
    
                startActivityForResult(i, CROPPED_PHOTO);
        }
    
       // Please ignore the case if there are more . . I know how to handle it 
    

    【讨论】:

    • #Shehbix @ error where is setUpcroppedFile() 方法我可以得到它的代码,因为我也面临同样的问题
    • @VikrantAlekar 对不起,我不再有这个代码,它是一个项目的过渡版本,但据我所知,它只是创建一个文件并返回一个句柄,就像这样:stackoverflow.com/questions/1239026/…
    • 没关系,我得到了我的解决方案,谢谢回复
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多