{"id":1426,"date":"2022-08-30T15:16:26","date_gmt":"2022-08-30T15:16:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/image-background-issue-android-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:26","modified_gmt":"2022-08-30T15:16:26","slug":"image-background-issue-android-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/image-background-issue-android-collection-of-common-programming-errors\/","title":{"rendered":"Image background issue android-Collection of common programming errors"},"content":{"rendered":"<p>i&#8217;m working on app, that reads image from gallery, and becomes background in layout, i&#8217;ve used the following code but it gives me force close error as soon as i click the picture, here is the code<\/p>\n<pre><code> Intent intent = new Intent();\n     intent.setType(\"image\/*\");\n     intent.setAction(Intent.ACTION_GET_CONTENT);\n     startActivityForResult(Intent.createChooser(intent, \"Select Picture\"),0);\n<\/code><\/pre>\n<p>and<\/p>\n<pre><code>@Override\n    protected void onActivityResult(int requestCode, int resultCode, Intent data) {\n\n        {\n            super.onActivityResult(requestCode, resultCode, data);\n\n        if(resultCode == Activity.RESULT_OK &amp;&amp; requestCode == 0) {\n            Uri photo = imageUri;\n            ContentResolver resolver = getContentResolver();\n            resolver.notifyChange(photo, null);\n\n            try {                   \n                Bitmap bitmap = MediaStore.Images.Media.getBitmap(resolver, photo);\n                RelativeLayout bg = (RelativeLayout) findViewById(R.id.bg);\n                Drawable drawable = new BitmapDrawable(getResources(), bitmap);\n                bg.setBackgroundDrawable(drawable);\n\n            \/\/Do something useful with your bitmap\n                } catch (FileNotFoundException e) {\n                e.printStackTrace();\n                } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n        }\n    }\n<\/code><\/pre>\n<p>main.xml<\/p>\n<pre><code>\n\n\n    \n\n        \n\n        \n    \n\n    \n\n\n<\/code><\/pre>\n<p>Here is my error log<\/p>\n<pre><code>05-16 20:20:23.768: W\/dalvikvm(288): threadid=1: thread exiting with uncaught exception (group=0x4001d800)\n05-16 20:20:24.098: E\/AndroidRuntime(288): FATAL EXCEPTION: main\n05-16 20:20:24.098: E\/AndroidRuntime(288): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content:\/\/media\/external\/images\/media\/1 }} to activity {org.example.touch\/org.example.touch.Touch}: java.lang.NullPointerException\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread.access$2800(ActivityThread.java:125)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.os.Handler.dispatchMessage(Handler.java:99)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.os.Looper.loop(Looper.java:123)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread.main(ActivityThread.java:4627)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at java.lang.reflect.Method.invokeNative(Native Method)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at java.lang.reflect.Method.invoke(Method.java:521)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at dalvik.system.NativeStart.main(Native Method)\n05-16 20:20:24.098: E\/AndroidRuntime(288): Caused by: java.lang.NullPointerException\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.os.Parcel.readException(Parcel.java:1253)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.os.Parcel.readException(Parcel.java:1235)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:458)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.content.ContentResolver.notifyChange(ContentResolver.java:850)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.content.ContentResolver.notifyChange(ContentResolver.java:836)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at org.example.touch.Touch.onActivityResult(Touch.java:117)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.Activity.dispatchActivityResult(Activity.java:3890)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)\n05-16 20:20:24.098: E\/AndroidRuntime(288):  ... 11 more\n<\/code><\/pre>\n<ol>\n<li>\n<p>It loads the image successfully and set as background, but crashes when i tried to load larger images i.e. 8mpx camera image etc. I managed to solve that by the following code:<\/p>\n<pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {\n\n        {\n            super.onActivityResult(requestCode, resultCode, data);\n\n            if (resultCode == Activity.RESULT_OK &amp;&amp; requestCode == 0) {\n                Uri photo = data.getData();\n                ContentResolver resolver = getContentResolver();\n                resolver.notifyChange(photo, null);\n\n                Display currentDisplay = getWindowManager().getDefaultDisplay();\n                int dw = currentDisplay.getWidth();\n                int dh = currentDisplay.getHeight() \/ 2 - 100;\n                try {\n                    \/\/ Load up the image's dimensions not the image itself\n                    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();\n                    bmpFactoryOptions.inJustDecodeBounds = true;\n                    Bitmap bmp = BitmapFactory.decodeStream(\n                            getContentResolver().openInputStream(photo), null,\n                            bmpFactoryOptions);\n                    int heightRatio = (int) Math\n                            .ceil(bmpFactoryOptions.outHeight \/ (float) dh);\n                    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth\n                            \/ (float) dw);\n                    if (heightRatio &gt; 1 &amp;&amp; widthRatio &gt; 1) {\n                        if (heightRatio &gt; widthRatio) {\n                            bmpFactoryOptions.inSampleSize = heightRatio;\n                        } else {\n                            bmpFactoryOptions.inSampleSize = widthRatio;\n                        }\n                    }\n                    bmpFactoryOptions.inJustDecodeBounds = false;\n                    FrameLayout bg = (FrameLayout) findViewById(R.id.frame);\n                    bmp = BitmapFactory.decodeStream(getContentResolver()\n                            .openInputStream(photo), null, bmpFactoryOptions);\n                    \/\/ bg.setImageBitmap(bmp);\n                    Drawable drawable = new BitmapDrawable(getResources(), bmp);\n                    bg.setBackgroundDrawable(drawable);\n                } catch (FileNotFoundException e) {\n                    Log.v(\"ERROR\", e.toString());\n                }\n            }\n        }\n    }\n<\/code><\/pre>\n<p>That worked perfectly fine for all images. Hope it would help someone!<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:50:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i&#8217;m working on app, that reads image from gallery, and becomes background in layout, i&#8217;ve used the following code but it gives me force close error as soon as i click the picture, here is the code Intent intent = new Intent(); intent.setType(&#8220;image\/*&#8221;); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, &#8220;Select Picture&#8221;),0); and @Override protected void onActivityResult(int requestCode, int resultCode, [&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-1426","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1426","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=1426"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1426\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}