【问题标题】:Where to call Crop Image Before Setting to ImageView在设置为 ImageView 之前在哪里调用裁剪图像
【发布时间】:2023-03-31 08:15:02
【问题描述】:

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

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

        }
    }
}

因为我也很好地压缩了我的图像......

【问题讨论】:

标签: java android image crop


【解决方案1】:

试试这个

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

【讨论】:

  • 我应该在哪里尝试这个...意味着在代码中的哪里,我没有得到它请帮助我
  • 您必须将图像发送到服务器的位置?
  • 如果您只需要为 imageview 裁剪图像,您可以使用 glide 库的覆盖方法简单地执行此操作,但如果您必须通过网络发送图像,则图像必须在您的存储中。解释一下你的情况。
  • 我想裁剪图像并发送到服务器,并将其设置为 Imageview 作为whatsapp
猜你喜欢
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2015-11-13
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多