簡體   English   中英

如何在Android中保存裁剪圖像而不是返回裁剪數據?

[英]How to save cropped image in android instead of returning cropping data?

我有一個應用程序,我可以讓用戶選擇一個圖庫圖像或用相機拍照,然后我給他選擇裁剪它。

現在,當用戶裁剪圖像時,圖庫裁剪器返回裁剪部件的ByteArray,是否可以傳遞一個參數,要求圖庫裁剪器將輸出保存到文件中(就像我開始捕獲圖像並在圖像中進行圖像的情況一樣) CAMER)

例如:

這是我使用的裁剪代碼:

// 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 

更新:問題已修復,我看到我會留下這個,以防其他人覺得它有用(請參閱上面代碼中的大評論)

此代碼是經過修改的代碼,它可以工作:

// 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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM