【问题标题】:Android crop image sizeAndroid 裁剪图像大小
【发布时间】:2013-04-04 09:44:21
【问题描述】:

我有以下代码供用户裁剪图像。当我将大小设置为超过 256 时,它不起作用。我的直觉是“cropIntent.putExtra("return-data", true);"导致错误。如何将 uri 传递给cropIIntent 并从onActivityResults 中检索出来?也就是裁剪后保存图片并检索。

private void performCrop() {
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(mImageCaptureUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 4);
        cropIntent.putExtra("aspectY", 3);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);

        startActivityForResult(cropIntent, PIC_CROP);

    } //respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        //display an error message
        String errorMessage = "Your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         if (requestCode == PIC_CROP) {
            try {
               final TextView imgTv = (TextView) findViewById(R.id.imageInfo);
                Bundle extras = data.getExtras();
                thumbnail = extras.getParcelable("data");
                ImageView image = (ImageView) findViewById(R.id.pestImage);
                image.setImageBitmap(thumbnail);
        File f = new File(mImageCaptureUri.getPath());
        if (f.exists()) {
            f.delete();
        }
    }
}//end onactivity results

【问题讨论】:

    标签: android crop


    【解决方案1】:
    private void performCrop() {
        try {
            //call the standard crop action intent (the user device may not support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
            cropIntent.setDataAndType(mImageCaptureUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 4);
            cropIntent.putExtra("aspectY", 3);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 800);
            cropIntent.putExtra("outputY", 800);
    
        File f = new File(Environment.getExternalStorageDirectory(),
                "/temporary_holder.jpg");
            try {
                f.createNewFile();
            } catch (IOException ex) {
            Log.e("io", ex.getMessage());  
            }
    
    uri = Uri.fromFile(f);
    
          cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    
            startActivityForResult(cropIntent, PIC_CROP);
    
        } //respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            //display an error message
            String errorMessage = "Your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    

    onActivityResult

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
             if (requestCode == PIC_CROP) {
    
                      String filePath = Environment.getExternalStorageDirectory()
                            + "/temporary_holder.jpg";
    
                    thumbnail = BitmapFactory.decodeFile(filePath);
    
    
    
                           //thumbnail =    BitmapFactory.decodeFile(filePath);
                   //    Log.i("",String.valueOf(thumbnail.getHeight()));
    
                        ImageView image = (ImageView) findViewById(R.id.pestImage);
                        image.setImageBitmap(thumbnail);
                        }}
    

    【讨论】:

      【解决方案2】:

      试试

          Uri cropedImageUri = data.getData();
      
      String[] filePathColumn = { MediaStore.Images.Media.DATA };
      
                      Cursor cursor = getContentResolver().query(cropedImage,
                              filePathColumn, null, null, null);
                      cursor.moveToFirst();
      
                      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                      String filePath = cursor.getString(columnIndex);
                      cursor.close();
      

      现在 filePath 是裁剪图像的路径

      从文件路径加载位图使用

       private Bitmap loadImageFromSDCard(String filePath) {
      
              BitmapFactory.Options bfo = new BitmapFactory.Options();
              bfo.inSampleSize = 1;
              bfo.outWidth = 100;
              bfo.outHeight = 100;
              Bitmap photo = BitmapFactory.decodeFile(filePath, bfo);
              return photo;
          } 
      

      【讨论】:

      • 我不需要将uri传入cropIntent?当我得到文件路径时,如何转换为位图?
      • 你能提供更多细节吗?
      • 我觉得你不需要传递一个Uri,看看resultCode是不是Success,我添加了从文件路径加载位图的代码。
      【解决方案3】:
          if (android.os.Build.VERSION.SDK_INT > 10){
              Intent cropIntent = new Intent("com.android.camera.action.CROP");
              cropIntent.setDataAndType(picUri, "image/*");
              cropIntent.putExtra("crop", "true");
              int gcd = BigInteger.valueOf(ImageWidth).
                    gcd(BigInteger.valueOf(ImageHeight)).intValue();
              cropIntent.putExtra("aspectX", (ImageWidth / gcd));
              cropIntent.putExtra("aspectY", (ImageHeight / gcd));
              cropIntent.putExtra("outputX", ImageWidth);  // X
              cropIntent.putExtra("outputY", ImageHeight);  // Y
              cropIntent.putExtra("return-data", true);
              cropIntent.setData(picUri);
              startActivityForResult(cropIntent, PIC_CROP_INTENT_ID);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 1970-01-01
        • 1970-01-01
        • 2011-11-11
        • 1970-01-01
        相关资源
        最近更新 更多