繁体   English   中英

如何在ImageView中显示图像

[英]How to display image in ImageView

所以我试图在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);
}

我在上面修改了我的方法调用,因此它为上述方法提供了文件而不是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上运行了该应用程序。

您是否尝试过将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"); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM