选择并修剪出在Android的最大尺寸 [英] Select and crop with a maximum size in android

查看:86
本文介绍了选择并修剪出在Android的最大尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做是显示裁剪框,但是它的最大尺寸是什么。类似的东西:

 意向意图=新的意图();
    intent.setType(图像/ *);
    intent.putExtra(裁剪,真);

    intent.putExtra(最大宽度,30);
    intent.putExtra(最大高度,30);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(意向,
            选择图片),IMAGE_SELECTED);
 

解决方案

试试这个:

  //照片的意图。
意向意图=新的意图(Intent.ACTION_GET_CONTENT,NULL);
intent.setType(图像/ *);

//意图参数。
intent.putExtra(裁剪,真);
intent.putExtra(aspectX,30); //这台最大宽度。
intent.putExtra(aspectY,30); //这台最大高度。
intent.putExtra(outputX,1);
intent.putExtra(outputY,1);
intent.putExtra(规模化,真正的);
intent.putExtra(回归数据,真正的);
intent.putExtra(MediaStore.EXTRA_OUTPUT,newImageFile());
intent.putExtra(OUTPUTFORMAT,Bitmap.Com pressFormat.JPEG.toString());

//发送意图。
activity.startActivityForResult(意向,BibleActivity.CROP_PHOTO_REQUEST_ code);
 

要获得新的映像文件:

  / **
 *创建裁剪后的图像新的临时文件。
 * @返回URI
 * /
公共乌里newImageFile(){
  返回Uri.fromFile(CREATEFILE(CROPPED_FILENAME));
}

/ **
 *创建文件。
 * @返回文件。
 * /
私人文件的CreateFile(字符串文件名){

  //确保我们有SD卡。
  如果(isSDCARDMounted()){

    //文件位置。
    档案文件=新的文件(activity.getExternalFilesDir(空),文件名);
    尝试 {
      //删除,如果存在。
      如果(file.exists())
        file.delete();

      //创建新的文件。
      file.createNewFile();

    }赶上(IOException异常E){
      Toast.makeText(活性,R.string.error_cannot_create_temp_file,Toast.LENGTH_LONG).show();
    }
    返回的文件;

  } 其他 {
    Toast.makeText(活动,R.string.error_no_sd_card_ present,Toast.LENGTH_LONG).show();
    返回null;
  }
}


/ **
 *检查,以确保我们有SD卡。
 * @返回
 * /
私人布尔isSDCARDMounted(){
  如果(Environment.getExternalStorageState()。等于(Environment.MEDIA_MOUNTED))
    返回true;
  其他
    返回false;
}
 

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

解决方案

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

这篇关于选择并修剪出在Android的最大尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆