Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:14:24+00:00 2026-06-10T04:14:24+00:00

i’m working on app, that reads image from gallery, and becomes background in layout,

  • 0

i’m working on app, that reads image from gallery, and becomes background in layout, i’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("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     startActivityForResult(Intent.createChooser(intent, "Select Picture"),0);

and

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        {
            super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == Activity.RESULT_OK && requestCode == 0) {
            Uri photo = imageUri;
            ContentResolver resolver = getContentResolver();
            resolver.notifyChange(photo, null);

            try {                   
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(resolver, photo);
                RelativeLayout bg = (RelativeLayout) findViewById(R.id.bg);
                Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                bg.setBackgroundDrawable(drawable);

            //Do something useful with your bitmap
                } catch (FileNotFoundException e) {
                e.printStackTrace();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
    }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.92"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.88"
            android:orientation="vertical" >

        </LinearLayout>
    </RelativeLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="143dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Save" />

</LinearLayout>

Here is my error log

05-16 20:20:23.768: W/dalvikvm(288): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-16 20:20:24.098: E/AndroidRuntime(288): FATAL EXCEPTION: main
05-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
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread.access$2800(ActivityThread.java:125)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.os.Looper.loop(Looper.java:123)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 20:20:24.098: E/AndroidRuntime(288):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 20:20:24.098: E/AndroidRuntime(288):  at java.lang.reflect.Method.invoke(Method.java:521)
05-16 20:20:24.098: E/AndroidRuntime(288):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 20:20:24.098: E/AndroidRuntime(288):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 20:20:24.098: E/AndroidRuntime(288):  at dalvik.system.NativeStart.main(Native Method)
05-16 20:20:24.098: E/AndroidRuntime(288): Caused by: java.lang.NullPointerException
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.os.Parcel.readException(Parcel.java:1253)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.os.Parcel.readException(Parcel.java:1235)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:458)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.content.ContentResolver.notifyChange(ContentResolver.java:850)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.content.ContentResolver.notifyChange(ContentResolver.java:836)
05-16 20:20:24.098: E/AndroidRuntime(288):  at org.example.touch.Touch.onActivityResult(Touch.java:117)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.Activity.dispatchActivityResult(Activity.java:3890)
05-16 20:20:24.098: E/AndroidRuntime(288):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
05-16 20:20:24.098: E/AndroidRuntime(288):  ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:14:26+00:00Added an answer on June 10, 2026 at 4:14 am

    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:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            {
                super.onActivityResult(requestCode, resultCode, data);
    
                if (resultCode == Activity.RESULT_OK && requestCode == 0) {
                    Uri photo = data.getData();
                    ContentResolver resolver = getContentResolver();
                    resolver.notifyChange(photo, null);
    
                    Display currentDisplay = getWindowManager().getDefaultDisplay();
                    int dw = currentDisplay.getWidth();
                    int dh = currentDisplay.getHeight() / 2 - 100;
                    try {
                        // Load up the image's dimensions not the image itself
                        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                        bmpFactoryOptions.inJustDecodeBounds = true;
                        Bitmap bmp = BitmapFactory.decodeStream(
                                getContentResolver().openInputStream(photo), null,
                                bmpFactoryOptions);
                        int heightRatio = (int) Math
                                .ceil(bmpFactoryOptions.outHeight / (float) dh);
                        int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
                                / (float) dw);
                        if (heightRatio > 1 && widthRatio > 1) {
                            if (heightRatio > widthRatio) {
                                bmpFactoryOptions.inSampleSize = heightRatio;
                            } else {
                                bmpFactoryOptions.inSampleSize = widthRatio;
                            }
                        }
                        bmpFactoryOptions.inJustDecodeBounds = false;
                        FrameLayout bg = (FrameLayout) findViewById(R.id.frame);
                        bmp = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(photo), null, bmpFactoryOptions);
                        // bg.setImageBitmap(bmp);
                        Drawable drawable = new BitmapDrawable(getResources(), bmp);
                        bg.setBackgroundDrawable(drawable);
                    } catch (FileNotFoundException e) {
                        Log.v("ERROR", e.toString());
                    }
                }
            }
        }
    

    That worked perfectly fine for all images. Hope it would help someone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.