在设置到ImageView之前在何处调用裁剪图像

时间:2017-12-05 13:55:56

标签: java android image crop

在设置到图像视图之前调用裁剪图像的位置,我没有得到它,因为我的图像没有被裁剪为此活动。因为我想裁剪图像并将裁剪后的图像也发布到服务器

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);
                imgNavHeaderPic.setImageBitmap(bitmap);
                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                createBitmapAndAssign(Uri.fromFile(file));
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == 2) {
            Uri selectedImage = data.getData();
            createBitmapAndAssign(data.getData());
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            imgNavHeaderPic.setImageBitmap(thumbnail);
            Drawable bitmap = imgNavHeaderPic.getDrawable();

        }
    }
}

因为我还能很好地压缩我的图像......

1 个答案:

答案 0 :(得分:0)

尝试这个

try{
    Bitmap bm = BitmapFactory.decodeResource(getResources(), 
    R.drawable.ic_launcher);
    bm = Bitmap.createBitmap(bm, 0, 0, 400, 400);
    your_imageview.setImageBitmap(bm);
}catch(Exception e){
      e.printStackTrace();     
}