ImageView的setImageBitmap不工作在某些设备上 [英] ImageView setImageBitmap not working on certain devices

查看:344
本文介绍了ImageView的setImageBitmap不工作在某些设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用练周围的照相机API 为此,我做了以下内容:

I was practicing around with the Camera API for which I did the following:

一个。设置一个目录作为捕获的图像( startActivityForResult

a. Setup a directory for the image captured (for startActivityForResult)

乙。设置位图,使图像可以显示一次保存的应用程序本身。

b. Setup the Bitmap so that the image could be shown once saved in the app itself.

这里的code以下:

设置的目录。

private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

在应用程序的全局变量

// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

// directory name to store the captured images
private static final String IMAGE_DIRECTORY_NAME = "my_camera_app";

private Uri fileUri;

// Views
ImageView photo;
Button camera;

摄像头实现逻辑

// Use camera function
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Successfully captured the image
            // display in imageview
            previewImage();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

private void previewImage() {
    try {
        // Bitmap factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // Downsizing image as it throws OutOfMemory exception for larger
        // images
        options.inSampleSize = 3;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        photo.setImageBitmap(bitmap);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

我遇到的问题是......对一些我所测试的应用程序的设备,应用程序显示拍摄的图像的空白preVIEW而在其他应用程序的工作原理完全好了。

The problem I am having is that ... for some of the devices that I tested the app, the app shows a blank preview of the image shot while in others the app works completely well.

为什么我会得到一个空白的反馈?及在一些情况下,当图像被保存时,用户不指向我的应用程序,而不是用户卡在相机应用

Why am I getting a blank feedback ? and in some of the cases, when an image is saved, the user is not directed to my app, instead the user is stuck in the camera app.

请做帮助。

推荐答案

至少在奇巧4.4.2在银河S4,相对布局,我不得不说我只是setImageBitmap上调用无效()在ImageView的。如果我没有,我得到了空白屏幕。加入setImageBitmap(后无效())后,然后我得到的图像。

At least for Kitkat 4.4.2 on a Galaxy S4, with a relative layout, I had to call invalidate() on the ImageView that i just setImageBitmap on. If i didn't, i got the blank screen. After adding the invalidate() after setImageBitmap(), then I got the image.

这篇关于ImageView的setImageBitmap不工作在某些设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆