{"id":1416,"date":"2022-08-30T15:16:21","date_gmt":"2022-08-30T15:16:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/high-quality-images-taken-from-phone-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:21","modified_gmt":"2022-08-30T15:16:21","slug":"high-quality-images-taken-from-phone-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/high-quality-images-taken-from-phone-error-collection-of-common-programming-errors\/","title":{"rendered":"high quality images taken from phone error-Collection of common programming errors"},"content":{"rendered":"<p>i am selecting in the gallery &#8220;BIG SIZE image and capture image high quality &#8221; then Force close error see also following code and error &#8230;Thanks<\/p>\n<p>code:<\/p>\n<pre><code> public void onActivityResult(int requestCode, int resultCode, Intent data) {\n            selectedImageUri = data.getData();\n            if (resultCode == RESULT_OK) {\n            \/\/  selectedImageUri = data.getData();\n                if (requestCode == SELECT_PICTURE) {\n\n    \/\/              selectedImageUri = data.getData();\n                    selectedImagePath = getPath(selectedImageUri);\n                    user_img.setImageURI(selectedImageUri);\n\n                    Log.e(\"select image from gallary \", \"\" + selectedImagePath);\n\n                } else if (requestCode == CAMERA_REQUEST &amp;&amp; resultCode == RESULT_OK) {\n\n                    \/\/Uri camrerauri = data.getData();\n                    selectedImagePath = getPath(selectedImageUri);\n                    user_img.setImageURI(selectedImageUri);\n                    Log.e(\"capture image \", \"\" + selectedImagePath);\n\n                }\n            }\n        }\n\n        public String getPath(Uri uri) {\n            String[] projection = { MediaStore.Images.Media.DATA };\n            Cursor cursor = managedQuery(uri, projection, null, null, null);\n            int column_index = cursor\n                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);\n            cursor.moveToFirst();\n            return cursor.getString(column_index);\n        }\n\n\n        public static int calculateInSampleSize(BitmapFactory.Options options,\n                int reqWidth, int reqHeight) {\n            \/\/ Raw height and width of image\n            final int height = options.outHeight;\n            final int width = options.outWidth;\n            int inSampleSize = 1;\n\n            if (height &gt; reqHeight || width &gt; reqWidth) {\n                if (width &gt; height) {\n                    inSampleSize = Math.round((float) height \/ (float) reqHeight);\n                } else {\n                    inSampleSize = Math.round((float) width \/ (float) reqWidth);\n                }\n            }\n            return inSampleSize;\n        }\n\n        public static Bitmap decodeSampledBitmapFromResource(int reqWidth,\n                int reqHeight) throws IOException {\n            int inSample = 8;\n\n\n            \/\/ First decode with inJustDecodeBounds=true to check dimensions\n            final BitmapFactory.Options options = new BitmapFactory.Options();\n            options.inJustDecodeBounds = true;\n            options.inSampleSize=inSample;\n            \/\/ BitmapFactory.decodeResource(res, resId, options);\n            \/\/BitmapFactory.decodeStream(selectedImageUri.openConnection().getInputStream(), null,options);\n            BitmapFactory.decodeFile(selectedImagePath, options);\n\n            \/\/ Calculate inSampleSize\n            options.inSampleSize = calculateInSampleSize(options, reqWidth,\n                    reqHeight);\n\n            \/\/ Decode bitmap with inSampleSize set\n            options.inJustDecodeBounds = false;\n            return BitmapFactory.decodeFile(selectedImagePath, options);\n            \/\/return BitmapFactory.decodeStream(new FlushedInputStream(selectedImageUri.openConnection().getInputStream()), null, options);\n        }\n\n        public static class FlushedInputStream extends FilterInputStream {\n            public FlushedInputStream(InputStream inputStream) {\n                super(inputStream);\n            }\n\n            @Override\n            public long skip(long n) throws IOException {\n                long totalBytesSkipped = 0L;\n                while (totalBytesSkipped &lt; n) {\n                    long bytesSkipped = in.skip(n - totalBytesSkipped);\n                    if (bytesSkipped == 0L) {\n                        int b = read();\n                        if (b &lt; 0) {\n                            break; \/\/ we reached EOF\n                        } else {\n                            bytesSkipped = 1; \/\/ we read one byte\n                        }\n                    }\n                    totalBytesSkipped += bytesSkipped;\n                }\n                return totalBytesSkipped;\n            }\n        }\n<\/code><\/pre>\n<p>error:<\/p>\n<pre><code> 11-16 13:03:56.269: E\/CursorWindow(11344): need to grow: mSize = 1048576, size = 1306040, freeSpace() = 1048426, numRows = 1\n    11-16 13:03:56.269: E\/CursorWindow(11344): Attempting to grow window beyond max size (1048576)\n    11-16 13:03:56.269: E\/Cursor(11344): Failed allocating 1306040 bytes for blob at 0,5\n    11-16 13:03:56.279: E\/CursorWindow(11344): Bad request for field slot 0,1. numRows = 0, numColumns = 6\n    11-16 13:03:56.279: W\/dalvikvm(11344): threadid=1: thread exiting with uncaught exception (group=0x40018560)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344): FATAL EXCEPTION: main\n    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\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.os.Handler.dispatchMessage(Handler.java:99)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.os.Looper.loop(Looper.java:123)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread.main(ActivityThread.java:3729)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at java.lang.reflect.Method.invokeNative(Native Method)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at java.lang.reflect.Method.invoke(Method.java:507)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at dalvik.system.NativeStart.main(Native Method)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.database.CursorWindow.getString_native(Native Method)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.database.CursorWindow.getString(CursorWindow.java:329)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at com.rememberme.BumpTest.onCreate(BumpTest.java:279)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)\n    11-16 13:03:56.279: E\/AndroidRuntime(11344):    ... 11 more\n<\/code><\/pre>\n<ol>\n<li>\n<p>Looks like you run out of memory. <strong>I solved this problem by writing the images to the sd before processing<\/strong>. I can&#8217;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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Try this:<\/p>\n<ul>\n<li>Load the image<\/li>\n<li>write a copy to the sd<\/li>\n<li>flush<\/li>\n<li>load the copy and try to process<\/li>\n<\/ul>\n<p>EDIT: Here you go&#8230; <strong>How to get large images from the camera and process the bitmap:<\/strong><\/p>\n<pre><code>rivate void openCamera() {\n    String _path = Environment.getExternalStorageDirectory()\n            + File.separator + \"temp.JPEG\";\n    tempFile = new File(_path);\n    outputFileUri = Uri.fromFile(tempFile);\n    bitmap = null;\n    \/\/ start default camera\n    Intent cameraIntent = new Intent(\n            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);\n    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,\n            outputFileUri);\n    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);\n}\n\n\nprivate void process() {\n    Bitmap editableBitmap = processor.processImage(bitmap);\n    imgView.setImageBitmap(editableBitmap);\n    \/\/ finish();\n}\n\n\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    ready = false;\n    if (requestCode == CAMERA_PIC_REQUEST) {\n        \/\/ bitmap = BitmapFactory.decodeFile(_path);\n        try {\n            bitmap = MediaStore.Images.Media.getBitmap(\n                    getApplicationContext().getContentResolver(),\n                    outputFileUri);\n        } catch (FileNotFoundException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        } catch (IOException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n        if (imgView == null) {\n            imgView = (ImageView) findViewById(R.id.imageView1);\n        }\n        process();\n    }\n}\n<\/code><\/pre>\n<p>Convert Bitmap to mutable bitmap so you can edit the pixels (I think I got this somwhere from pastebin but it&#8217;s no magic)<\/p>\n<pre><code>public static Bitmap convertToMutable(Bitmap imgIn) {\n        try {\n            \/\/ this is the file going to use temporally to save the bytes.\n            \/\/ This file will not be a image, it will store the raw image data.\n            File file = new File(Environment.getExternalStorageDirectory()\n                    + File.separator + \"temp.tmp\");\n\n            \/\/ Open an RandomAccessFile\n            \/\/ Make sure you have added uses-permission\n            \/\/ android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"\n            \/\/ into AndroidManifest.xml file\n            RandomAccessFile randomAccessFile = new RandomAccessFile(file, \"rw\");\n\n            \/\/ get the width and height of the source bitmap.\n            int width = imgIn.getWidth();\n            int height = imgIn.getHeight();\n            Config type = imgIn.getConfig();\n\n            \/\/ Copy the byte to the file\n            \/\/ Assume source bitmap loaded using options.inPreferredConfig =\n            \/\/ Config.ARGB_8888;\n            FileChannel channel = randomAccessFile.getChannel();\n            MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0,\n                    imgIn.getRowBytes() * height);\n            imgIn.copyPixelsToBuffer(map);\n            \/\/ recycle the source bitmap, this will be no longer used.\n            imgIn.recycle();\n            System.gc();\/\/ try to force the bytes from the imgIn to be released\n\n            \/\/ Create a new bitmap to load the bitmap again. Probably the memory\n            \/\/ will be available.\n            imgIn = Bitmap.createBitmap(width, height, type);\n            map.position(0);\n            \/\/ load it back from temporary\n            imgIn.copyPixelsFromBuffer(map);\n            \/\/ close the temporary file and channel , then delete that also\n            channel.close();\n            randomAccessFile.close();\n\n            \/\/ delete the temp file\n            file.delete();\n\n        } catch (FileNotFoundException e) {\n            e.printStackTrace();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n\n        return imgIn;\n    }\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:49:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i am selecting in the gallery &#8220;BIG SIZE image and capture image high quality &#8221; then Force close error see also following code and error &#8230;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1416","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1416"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1416\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}