路径图像不会加载

时间:2016-10-16 11:26:20

标签: android android-intent camera

我有这个听众:

@Override
    public void onClick(View v) {
        showDialog();
        ImageButton currentButton = (ImageButton)v;
        Bitmap bmp = BitmapFactory.decodeFile(mCurrentPhotoPath);
        currentButton.setImageBitmap(bmp);

    }

图片已在showDialog()中成功拍摄,但在此之后它不会设为imageButton。它变白了。生成的路径是: file:/storage........../filename.jpg已将其保存在mCurrentPhotoPath中 我试图在拍摄之后设置来自drawable的图像以尝试它并且它有效。出于某种原因,我无法将图像设置为ImageButton。我是否必须调整大小(照片是全尺寸的)? 在THIS教程后我做了这个:

@Override
public void onClick(View v) {
    showDialog((ImageButton)v);
    setPic((ImageButton)v);
}

private void setPic(ImageButton mImageView) {
    if (shotFired){
        int targetW = mImageView.getWidth();
        int targetH = mImageView.getHeight();
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
        mImageView.setImageBitmap(bitmap);
        mImageView.setTag(mCurrentPhotoPath);
        shotFired=false;

    }

似乎我没有重新拍摄setPic()而只是打电话给那个旧的我已经让它发挥作用。必须是时间安排......我需要让这件事有效。

2 个答案:

答案 0 :(得分:1)

我相信你的问题是因为在将内存加载到内存时正常发生的outOfMEmory问题。

在android中加载位图的最佳做法是在加载之前找到位图尺寸。然后加载合适大小的位图取决于您的设备内存大小和屏幕大小。

我强烈建议您阅读这个有关developpe.android网站的精彩教程。这将帮助您全面了解位图的工作原理,并了解加载它们的最佳实践,还包括源代码。请仔细阅读所有这些教程。

https://developer.android.com/training/displaying-bitmaps/index.html

https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

https://developer.android.com/training/displaying-bitmaps/display-bitmap.html

我建议您阅读这些链接,但如果您还没有兴趣,可以使用Glide或Piccasso等库来加载您的位图。它们将帮助您在没有OutOfMemoty错误的情况下加载。

答案 1 :(得分:1)

如果您使用相机拍摄照片以将其设置为ImageView,则需要在使用相机拍摄图像后在onActivityResult()中设置图像。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        //set the image to imageView


    } else if (resultCode == Activity.RESULT_CANCELED) {
        //image was not taken 
        //show some error message

    }

}
相关问题