1

I'm trying to get a program to let the user to import a custom background.

Here's where I'm at:

I have the getDrawable function taking another function as an argument:

mDrawableBg = getResources().getDrawable(getImage());   

getImage() is suppose to return a integer referencing the selected image, here is the code (so far) for that function:

public int getImage(){

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
    intent.setType("image/*");
    startActivityForResult(intent, 10);

}

This is suppose to open the gallery and let the user select an image. I would then use mDrawableBg to set the background. I'm not sure how to return a reference ID to that selected image though. Any suggestions?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Mike
  • 825
  • 3
  • 12
  • 30
  • 1
    [Get thumbnail Uri/path of the image stored in sd card + android](http://stackoverflow.com/questions/5548645/get-thumbnail-uri-path-of-the-image-stored-in-sd-card-android) have you tried this. – BBdev Jul 25 '12 at 05:58
  • These return Strings as paths, is there any way I'd be able to use these to set the background with a Drawable object? getDrawable() only takes integer arguments. – Mike Jul 25 '12 at 06:15

3 Answers3

2

Try this:

    String pathName = "selected Image path";
    Resources res = getResources();
    Bitmap bitmap = BitmapFactory.decodeFile(pathName);
    BitmapDrawable bd = new BitmapDrawable(res, bitmap);
    View view = findViewById(R.id.container);
    view.setBackgroundDrawable(bd);
JiTHiN
  • 6,548
  • 5
  • 43
  • 69
  • I'm not sure where I would put this. I'm pretty new at dealing with Android, I'm still getting used to how everything is laid out. Is there any way to directly modify the function I have above to return a reference ID of the selected image? – Mike Jul 25 '12 at 06:05
1

I'm not sure, but if you mean you don't know how to receive results from that intent, you can use :

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) 
        {
            if (requestCode == 10)
            {
                // DoSomething
            }
        }
    }
Namnamseo
  • 187
  • 1
  • 15
  • I'm not too sure about dealing with Android in general, this is my first attempt at programming something in Android. I'm just trying to figure out how I can modify the getImage() function above to return a reference ID to an image that a user selects from the phone's gallery. – Mike Jul 25 '12 at 06:08
  • 1
    Do you mean you want the getImage() function to return the image or something? Well, the result of startActivity() goes to onActivityResult. That means, if you call startActivity(), the function getImage() ends, and when the user selects the image, the function 'onActivityResult()' is called. – Namnamseo Jul 25 '12 at 06:15
  • Alright, so does that mean that I would be able to somehow override onActivityResult() to return this reference ID? Is it even possible to return a reference ID of an image that is stored on the phone's internal/flash memory? – Mike Jul 25 '12 at 06:19
  • The result of selecting image from gallery is in the variable 'data'. You can get the Drawable by Drawable.createFromPath(path) – Namnamseo Jul 25 '12 at 06:28
  • I don't know what you mean by reference ID. If you mean something stored in the class R, that is the app's resources. – Namnamseo Jul 25 '12 at 06:29
  • I was looking for a way to create the background by linking the selected image to the Drawable object (I guess createFramePath would worK). Thanks for the help. – Mike Jul 25 '12 at 06:43
1

The way you're attempting to do it is not possible, I'm afraid. One of the things you'll want to learn as a new Android developer is how the cycle between activities works. In your case, you're running an Activity that calls upon an Intent to get data from it. However, in the Android API, an Intent can only be referenced on its own time. This means you can't use your getImage() method the way you had tried.

There is hope, though!

What you first need to do is call the Intent. You will do this through the code you have now in getImage():

public void getImage() { // This has to be a void!
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
    intent.setType("image/*");
    startActivityForResult(intent, 10);
}

This method will now start the Image Picker that you want users to select from. Next, you have to catch what is returned. This cannot be returned from your getImage() method, but instead must be collected from elsewhere.

You must implement the below method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        final int SELECT_PICTURE = 1; // Hardcoded from API
        if (requestCode == SELECT_PICTURE) {
            String pathToImage = data.getData().getPath(); // Get path to image, returned by the image picker Intent
            mDrawableBg = Drawable.createFromPath(pathToImage); // Get a Drawable from the path
        }
    }
}

Lastly, instead of calling mDrawableBg = getResources().getDrawable(getImage());, just call getImage();. This will initialize the Image Picker.

Some reading:

Good luck!

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • One question: When you set pathToImage to getPath(data.getData()), is this the same as (data.getData()).getPath() ? There was no way for me to use getPath the way you had it up there. – Mike Jul 25 '12 at 16:47
  • Oops.. yes, it should be. Sorry about that. – Cat Jul 25 '12 at 17:28