应用程序崩溃在imageView上显示解码图像

时间:2013-10-30 16:29:22

标签: android

在我的应用程序中,当我尝试在不使用压缩技术的情况下在图像视图中显示图像时,我的应用程序崩溃了。以下是完整的代码和logcat

10-30 21:19:53.779: E/Trace(1817): error opening trace file: No such file or directory (2)
10-30 21:20:12.789: E/AndroidRuntime(1817): FATAL EXCEPTION: main
10-30 21:20:12.789: E/AndroidRuntime(1817): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data dat=content (has extras) }} to activity {com.example.checkactivities/com.example.checkactivities.Activity2}: java.nio.BufferUnderflowException
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3672)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread.access$1100(ActivityThread.java:151)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.os.Looper.loop(Looper.java:155)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread.main(ActivityThread.java:5454)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at dalvik.system.NativeStart.main(Native Method)
10-30 21:20:12.789: E/AndroidRuntime(1817): Caused by: java.nio.BufferUnderflowException
10-30 21:20:12.789: E/AndroidRuntime(1817):     at java.nio.Buffer.checkGetBounds(Buffer.java:177)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:63)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at com.example.checkactivities.Activity2.onActivityResult(Activity2.java:476)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.Activity.dispatchActivityResult(Activity.java:5275)
10-30 21:20:12.789: E/AndroidRuntime(1817):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3618)
10-30 21:20:12.789: E/AndroidRuntime(1817):     ... 11 more


        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
        int bytes =  thumbnail.getWidth()*thumbnail.getHeight()*4;
        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
            thumbnail.copyPixelsToBuffer(buffer);
        byte[] bytes1 = new byte[bytes];
        buffer.get(bytes1, 0, bytes1.length);
        byte[] b = buffer.array();
        String ImageString = Base64.encodeToString(b, Base64.DEFAULT);
        byte[] bytarray = Base64.decode(ImageString, Base64.DEFAULT);
         Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,bytarray.length);
         imageView11.setImageBitmap(bmimage);

1 个答案:

答案 0 :(得分:0)

您可以直接使用Bitmap之类的

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView11.setImageBitmap(thumbnail); 

您可以首先解码图像,然后像

一样使用它
Uri mImageCaptureUri = intent.getData();
                try {
                    thumbnail = decodeBitmap(mImageCaptureUri, this);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                imageView11.setImageBitmap(thumbnail);

图片解码

public Bitmap decodeBitmap(Uri selectedImage, Context context)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(context.getContentResolver()
                .openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        System.out.println("width_tmp" + width_tmp + "height_tmp" + height_tmp);
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp = 50;
            height_tmp = 50;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(context.getContentResolver()
                .openInputStream(selectedImage), null, o2);
    }