如何在ImageView中显示图像

时间:2013-10-11 16:36:14

标签: android imageview android-camera android-imageview android-image

所以我试图在ImageView中显示捕获的图像但由于某种原因我的ImageView保持黑色。如果我从drawable-Folder中选择一个图像就可以了。捕获的图像也存在于右侧文件夹中。

相机意图:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

private Uri getImageUri() {
    String CAPTURE_TITLE="3.png";
    File file = new File(Environment.getExternalStorageDirectory() + "/receipts", CAPTURE_TITLE);
    Uri imgUri = Uri.fromFile(file);

    return imgUri;
}

这些是我尝试的东西:

1

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        File file = new File(Environment.getExternalStorageDirectory()+File.separator + "receipts"+File.separator + "3.jpg");
        ImageView receiptImage = (ImageView) findViewById(R.id.result_image);
        receiptImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));
    }
}

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }

    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }


    options.inSampleSize = inSampleSize;

    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}

我修改了上面的方法调用,因此它为上面的方法提供了一个File而不是一个Uri。

2

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        ImageView receiptImage = (ImageView) findViewById(R.id.result_image);

        Uri receiptUri = Uri.parse("/receipts/3.png");

        try {
            InputStream inputStream = getContentResolver().openInputStream(receiptUri);
            Drawable drawable = Drawable.createFromStream(inputStream, null);
            receiptImage .setImageDrawable(drawable);
        } catch (FileNotFoundException e) {}
    }
}

3

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        ImageView receiptImage = (ImageView) findViewById(R.id.result_image);
        receiptImage.setImageBitmap(BitmapFactory.decodeFile("/receipts/3.png"));  
    }
}

我也尝试使用Bitmap photo = (Bitmap) data.getExtras().get("data");,但我的应用在相机应用关闭后立即崩溃。由于某种原因,数据返回null。这是崩溃日志:

10-14 10:38:10.728  32275-32275/com.example.debt E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.debt/com.example.debt.ScanReceiptActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3184)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3227)
            at android.app.ActivityThread.access$1100(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.debt.ScanReceiptActivity.onActivityResult(ScanReceiptActivity.java:78)
            at android.app.Activity.dispatchActivityResult(Activity.java:5375)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3180)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3227)
            at android.app.ActivityThread.access$1100(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)

我在LG P880上运行了应用程序。

1 个答案:

答案 0 :(得分:0)

您是否尝试将CAMERA_PIC_REQUEST更改为CAMERA_REQUEST

Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());

startActivityForResult(cameraIntent, CAMERA_REQUEST);

捕获图像后,您将在ActivtyResult中获得结果。

if (requestCode == CAMERA_REQUEST) {  
    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
}