【问题标题】:Fit just taken photo into imagebutton将刚刚拍摄的照片放入图像按钮
【发布时间】:2013-05-04 18:40:48
【问题描述】:

我有一个ImageButton,当我按下它时,我可以用我的手机拍照。当我拍照时,ImageButtons 将图片作为其来源。但问题是,图像没有按比例缩小到 ImageButton 的尺寸。它只是在 Imagebutton 中显示图像的一部分,而不是按比例缩小。在做了一些研究之后,我发现你必须使用平局 9 补丁。但在这种情况下这是不可能的,因为图像不在我的资源中,它们是由用户自己制作的。

有人可以帮我解决这个问题吗?

我的代码:

if (requestCode == REQUEST_CAMERA) {
    File f = new File(Environment.getExternalStorageDirectory().toString());
    for (File temp : f.listFiles()) {
        if (temp.getName().equals("temp.jpg")) {
            f = temp;
            break;
        }
    }

    try {
        Bitmap bm;
        BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
        bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
        // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
        btnImg.setImageBitmap(bm);
        String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
        f.delete();
        OutputStream fOut = null;
        File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");

        try {
                fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
            e.printStackTrace();
        }
            }
} 
else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, MainActivity.this);
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
btnImg.setImageBitmap(bm);
}

【问题讨论】:

    标签: android android-camera image imagebutton nine-patch


    【解决方案1】:

    使用 ImageView 的方法setScaleType。传递ScaleType.CENTER_INSIDE 作为参数。

    btnImg.setImageBitmap(bm);
    btnImg.setScaleType(ScaleType.CENTER_INSIDE);
    

    【讨论】:

    • 这对我有用,但现在,在插入第二张图片后,应用程序崩溃了。这是相关的还是我应该为此提出一个新问题?
    • 必须是 OutOfMemoryException。添加一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多