0

所以我试图在 ImageView 中显示捕获的图像,但由于某种原因,我的 ImageView 保持黑色。如果我从可绘制文件夹中选择一个图像,它就可以工作。捕获的图像也存在于正确的文件夹中。

相机意图:

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 上运行该应用程序。

4

1 回答 1

0

您是否尝试过更改CAMERA_PIC_REQUESTCAMERA_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"); 
}
于 2013-10-11T17:08:46.700 回答