开发者

Select and crop with a maximum size in android

开发者_开发知识库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;
}


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜