从手机错误中获取的高质量图像

时间:2012-11-16 07:34:52

标签: android image sqlite

我在图库中选择“BIG SIZE图像并捕获图像高质量”然后强制关闭错误另见代码和错误...谢谢

代码:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            selectedImageUri = data.getData();
            if (resultCode == RESULT_OK) {
            //  selectedImageUri = data.getData();
                if (requestCode == SELECT_PICTURE) {

    //              selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    user_img.setImageURI(selectedImageUri);

                    Log.e("select image from gallary ", "" + selectedImagePath);

                } else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

                    //Uri camrerauri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    user_img.setImageURI(selectedImageUri);
                    Log.e("capture image ", "" + selectedImagePath);

                }
            }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }


        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float) height / (float) reqHeight);
                } else {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
                }
            }
            return inSampleSize;
        }

        public static Bitmap decodeSampledBitmapFromResource(int reqWidth,
                int reqHeight) throws IOException {
            int inSample = 8;


            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize=inSample;
            // BitmapFactory.decodeResource(res, resId, options);
            //BitmapFactory.decodeStream(selectedImageUri.openConnection().getInputStream(), null,options);
            BitmapFactory.decodeFile(selectedImagePath, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth,
                    reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(selectedImagePath, options);
            //return BitmapFactory.decodeStream(new FlushedInputStream(selectedImageUri.openConnection().getInputStream()), null, options);
        }

        public static class FlushedInputStream extends FilterInputStream {
            public FlushedInputStream(InputStream inputStream) {
                super(inputStream);
            }

            @Override
            public long skip(long n) throws IOException {
                long totalBytesSkipped = 0L;
                while (totalBytesSkipped < n) {
                    long bytesSkipped = in.skip(n - totalBytesSkipped);
                    if (bytesSkipped == 0L) {
                        int b = read();
                        if (b < 0) {
                            break; // we reached EOF
                        } else {
                            bytesSkipped = 1; // we read one byte
                        }
                    }
                    totalBytesSkipped += bytesSkipped;
                }
                return totalBytesSkipped;
            }
        }

错误:

 11-16 13:03:56.269: E/CursorWindow(11344): need to grow: mSize = 1048576, size = 1306040, freeSpace() = 1048426, numRows = 1
    11-16 13:03:56.269: E/CursorWindow(11344): Attempting to grow window beyond max size (1048576)
    11-16 13:03:56.269: E/Cursor(11344): Failed allocating 1306040 bytes for blob at 0,5
    11-16 13:03:56.279: E/CursorWindow(11344): Bad request for field slot 0,1. numRows = 0, numColumns = 6
    11-16 13:03:56.279: W/dalvikvm(11344): threadid=1: thread exiting with uncaught exception (group=0x40018560)
    11-16 13:03:56.279: E/AndroidRuntime(11344): FATAL EXCEPTION: main
    11-16 13:03:56.279: E/AndroidRuntime(11344): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rememberme/com.rememberme.BumpTest}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.os.Handler.dispatchMessage(Handler.java:99)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.os.Looper.loop(Looper.java:123)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread.main(ActivityThread.java:3729)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at java.lang.reflect.Method.invoke(Method.java:507)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at dalvik.system.NativeStart.main(Native Method)
    11-16 13:03:56.279: E/AndroidRuntime(11344): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.database.CursorWindow.getString_native(Native Method)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.database.CursorWindow.getString(CursorWindow.java:329)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at com.rememberme.BumpTest.onCreate(BumpTest.java:279)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    11-16 13:03:56.279: E/AndroidRuntime(11344):    ... 11 more

1 个答案:

答案 0 :(得分:1)

看起来你内存不足。 我通过在处理之前将图像写入sd解决了这个问题。我现在不能发送代码但稍后。实际处理图像需要大量内存。因此,您应该在处理图像之前考虑调整位图大小。

提示: 您可能无法写入位图。您必须复制该位图,以便访问和编辑像素。

这就是为什么我必须将位图的副本复制到我的sd,因为将副本写入内存会导致异常。

试试这个:

  • 加载图片
  • 将副本写入sd
  • 冲洗
  • 加载副本并尝试处理

编辑: 你去... 如何从相机获取大图像并处理位图:

rivate void openCamera() {
    String _path = Environment.getExternalStorageDirectory()
            + File.separator + "temp.JPEG";
    tempFile = new File(_path);
    outputFileUri = Uri.fromFile(tempFile);
    bitmap = null;
    // start default camera
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
            outputFileUri);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}


private void process() {
    Bitmap editableBitmap = processor.processImage(bitmap);
    imgView.setImageBitmap(editableBitmap);
    // finish();
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ready = false;
    if (requestCode == CAMERA_PIC_REQUEST) {
        // bitmap = BitmapFactory.decodeFile(_path);
        try {
            bitmap = MediaStore.Images.Media.getBitmap(
                    getApplicationContext().getContentResolver(),
                    outputFileUri);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (imgView == null) {
            imgView = (ImageView) findViewById(R.id.imageView1);
        }
        process();
    }
}

将Bitmap转换为可变位图,这样你就可以编辑像素(我想我从pastebin那里得到了这个,但这并不神奇)

public static Bitmap convertToMutable(Bitmap imgIn) {
        try {
            // this is the file going to use temporally to save the bytes.
            // This file will not be a image, it will store the raw image data.
            File file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "temp.tmp");

            // Open an RandomAccessFile
            // Make sure you have added uses-permission
            // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
            // into AndroidManifest.xml file
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

            // get the width and height of the source bitmap.
            int width = imgIn.getWidth();
            int height = imgIn.getHeight();
            Config type = imgIn.getConfig();

            // Copy the byte to the file
            // Assume source bitmap loaded using options.inPreferredConfig =
            // Config.ARGB_8888;
            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0,
                    imgIn.getRowBytes() * height);
            imgIn.copyPixelsToBuffer(map);
            // recycle the source bitmap, this will be no longer used.
            imgIn.recycle();
            System.gc();// try to force the bytes from the imgIn to be released

            // Create a new bitmap to load the bitmap again. Probably the memory
            // will be available.
            imgIn = Bitmap.createBitmap(width, height, type);
            map.position(0);
            // load it back from temporary
            imgIn.copyPixelsFromBuffer(map);
            // close the temporary file and channel , then delete that also
            channel.close();
            randomAccessFile.close();

            // delete the temp file
            file.delete();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return imgIn;
    }