Mike
Mike

Reputation: 825

Returning Image Resource ID

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?

Upvotes: 1

Views: 1137

Answers (3)

Cat
Cat

Reputation: 67502

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!

Upvotes: 1

JiTHiN
JiTHiN

Reputation: 6588

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);

Upvotes: 2

Namnamseo
Namnamseo

Reputation: 187

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
            }
        }
    }

Upvotes: 1

Related Questions