high quality images taken from phone error-Collection of common programming errors
i am selecting in the gallery “BIG SIZE image and capture image high quality ” then Force close error see also following code and error …Thanks
code:
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;
}
}
error:
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
-
Looks like you run out of memory. I solved this problem by writing the images to the sd before processing. I can’t send you the code right now but later. Acutally processing images takes lots of memory. So you should consider resizing the bitmap before you process the image.
Hint: You might not be able to write to the bitmap. You have to make a copy of that bitmap so you can access and edit the pixels.
This is why I had to make a copy of the bitmap to my sd, because writing the copy to the memory would cause an exception.
Try this:
- Load the image
- write a copy to the sd
- flush
- load the copy and try to process
EDIT: Here you go… How to get large images from the camera and process the bitmap:
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(); } }
Convert Bitmap to mutable bitmap so you can edit the pixels (I think I got this somwhere from pastebin but it’s no magic)
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; }
Originally posted 2013-11-16 20:49:12.