在android中选择并裁剪最大尺寸

Posted

技术标签:

【中文标题】在android中选择并裁剪最大尺寸【英文标题】:Select and crop with a maximum size in android 【发布时间】:2011-02-21 15:56:56 【问题描述】:

我想做的是显示裁剪框,但有最大尺寸。类似的东西:

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra("crop", "true");

    intent.putExtra("max-width", 30);
    intent.putExtra("max-height", 30);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,
            "Select Picture"), IMAGE_SELECTED);

【问题讨论】:

在使用 android 提供的裁剪意图时,查看这些答案可能是个好主意:***.com/questions/12758425/… 【参考方案1】:

试试这个:

// Photo intent.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");

// Intent parameters.
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 30); // This sets the max width.
intent.putExtra("aspectY", 30); // This sets the max height.
intent.putExtra("outputX", 1);
intent.putExtra("outputY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageFile());
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

// Send intent.
activity.startActivityForResult(intent, BibleActivity.CROP_PHOTO_REQUEST_CODE);

要获取新的图像文件:

/**
 * Create new temporary file for cropped image.
 * @return uri
 */
public Uri newImageFile() 
  return Uri.fromFile(createFile(CROPPED_FILENAME));        


/**
 * Create file.
 * @return file.
 */
private File createFile(String fileName) 

  // Make sure we have SD Card.
  if (isSDCARDMounted()) 

    // File location.
    File file = new File(activity.getExternalFilesDir(null), fileName);
    try 
      // Delete if exist.
      if (file.exists())
        file.delete();

      // Create new file.
      file.createNewFile();

     catch (IOException e) 
      Toast.makeText(activity, R.string.error_cannot_create_temp_file, Toast.LENGTH_LONG).show();
    
    return file;

   else 
    Toast.makeText(activity, R.string.error_no_sd_card_present, Toast.LENGTH_LONG).show();
    return null;
  



/**
 * Check to make sure we have SD Card.
 * @return
 */
private boolean isSDCARDMounted() 
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    return true;
  else
    return false;

【讨论】:

【参考方案2】:

试试这个,解决图片被黑框裁剪的问题。

Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scaleUpIfNeeded", true);//learn it from gallery2 source code
intent.putExtra("max-width", 30);
intent.putExtra("max-height", 30);

intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
        "Select Picture"), IMAGE_SELECTED);

【讨论】:

【参考方案3】:

尝试添加额外的:

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

并设置输出文件

File f = new File(Environment.getExternalStorageDirectory(),
            "/temporary_holder.jpg");
    try 
        f.createNewFile();
     catch (IOException ex) 
        Log.e("io", ex.getMessage());
    

    Uri uri = Uri.fromFile(f);
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri)

然后在ActivityResult上检索文件(不是数据)

String filePath = Environment.getExternalStorageDirectory()
                    + "/temporary_holder.jpg";
            cropBitmap = BitmapFactory.decodeFile(filePath);

【讨论】:

以上是关于在android中选择并裁剪最大尺寸的主要内容,如果未能解决你的问题,请参考以下文章

使用“com.android.camera.action.CROP”意图裁剪图像,在某些设备上返回小尺寸位图

避免图像裁剪并将其投射到固定的图像尺寸

在 Android 中裁剪以适应图像

android:从图库中选择图像,然后裁剪并显示在图像视图中

被父级裁剪的 Android 视图的全尺寸

Android - 如何使用视图布局尺寸裁剪 jpeg