【问题标题】:Image from path wont load路径中的图像不会加载
【发布时间】:2016-10-16 11:26:20
【问题描述】:

我得到了这个监听器:

@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() 而不进行新的拍摄,而只是调用旧的我让它工作。一定是及时的……我需要让这件事发挥作用。

【问题讨论】:

  • 你是从 showDialog 启动相机吗??
  • “生成的路径是:file:/storage........../filename.jpg”——这不是路径。那也不是有效的Uri,所以我不知道那是什么。但是,decodeFile() 不会识别它。
  • 我按照我刚刚发布的教程做了所有这些。
  • @sanjeetkumarSingh 是的,我是
  • 有什么想法吗?还是不行

标签: android android-intent camera


【解决方案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

    }

}

【讨论】:

  • 我的错。我在调用弹出窗口后调用了setPic(),而不是onActivityResult()。现在它的工作
【解决方案2】:

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

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

我强烈建议您阅读 developer.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 错误。

【讨论】:

  • 我做到了,但我仍然有同样的问题
  • 这不是我的解决方案,但使它成为可能。赞成
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 2017-04-07
  • 2017-08-15
  • 2016-08-12
  • 2022-11-20
  • 2017-09-04
  • 1970-01-01
  • 2019-04-08
相关资源
最近更新 更多