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

发布于 2024-10-18 07:51:06 字数 392 浏览 4 评论 0原文

我想做的是显示裁剪框,但具有最大尺寸。像这样的东西:

    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);

What I want to do it's to show the crop box, but with a maximum size. Something like that:

    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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇妻 2024-10-25 07:51:06

试试这个:

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

Try this:

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

To get the new image file:

/**
 * 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;
}
浅浅淡淡 2024-10-25 07:51:06

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

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);

try this, solve problem that image cropped with black frame.

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);
梨涡少年 2024-10-25 07:51:06

尝试添加额外的:

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);

try to add the extra :

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

and set the outPut file

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)

then retrieve the file ( not the data ) onActivityResult

String filePath = Environment.getExternalStorageDirectory()
                    + "/temporary_holder.jpg";
            cropBitmap = BitmapFactory.decodeFile(filePath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文