0

I am having a simple problem and have searched around, but no luck so far. (Soundboard type application - only one button so far)

Currently, I click the "soundboard" button on my home screen and it leads to my one button trial soundboard- I have an "addimage" button on top of the page. Underneath the button is a blank image button (to be filled). upon clicking "addimage", I am taken to the gallery where I select an image. After selection, the image is set as the background in the imagebutton (source becomes background). After much trial and error, I have finally knocked down the scaling issue (if selected image was big, it blew up inside the button).

Now my issue is getting the selected image to "stick" to the button. If I click the back button to go back to the home on the bottom bar or click the back button on the action bar (from the soundboard page) --- and the go back to the soundboard after--- the previously selected image which was set to the imagebutton is not there anymore (seems image button is reset or reinitialized). Is there a way I can get that selected image to stick and not erase/clear if I go to a different page or exit? Not sure how this would be handled

Included is a snippet of the code for setting the selected image from gallery to the imagebutton and perform some sort of scaling. I couldn't post a picture...but I hope you can imagine...very simple.

Thank You!

PS: The eventual goal is to then use soundpool and a small audio clip in raw folder and link that to the imagebutton above - audio output.

        ImageButton imagebutton = (ImageButton) findViewById(R.id.imBut);

        Bitmap bitmaporg = BitmapFactory.decodeFile(picturePath);
        bitmaporg = Bitmap.createScaledBitmap(bitmaporg,350,350,true);

        imagebutton.setImageBitmap(bitmaporg);
Only1Swami
  • 11
  • 3
  • do you want the picture to be there every time you open the app to use it, or just the one time that the app is open? – JRowan Mar 03 '13 at 05:10
  • I would like to have it so it is there everytime I open the app. Also, If I want to change the image, I can click the "addimage" button again and it would override or replace the old image. Kind of like "dynamically" setting the image based on the user... Thanks! – Only1Swami Mar 03 '13 at 05:13
  • save the path to a text file, and then everytime that activity opens up load the path and then check if the path isnt null load the image to the imagebutton – JRowan Mar 03 '13 at 05:15
  • Thank you for the reply. Would it be possible to provide a line or two of sample code? This would really help. Sorry, I am new developing so any help/sample would be really appreciated! – Only1Swami Mar 03 '13 at 05:17
  • ok give me a little bit – JRowan Mar 03 '13 at 05:17

2 Answers2

0

You would want to save the resized image for the button locally, so as to be used later. Even after closing and starting again.

What's happening here is a new instance of the activity (with button) is being initialized, which doesn't have info about the image being applied to some other button instance.

What you want is when initializing the activity (activity's constructor) check if there is image saved (which you only will save), and if it is there apply image. See this SO question, it has nice little nifty answer. (You already know how to fetch data from a given path).

Else, if you want image to persist only between run you may want to save the image source as some global variable and then you may pass the image source to the new activity through the intent or directly use it in new activity as it is already global variable.

For global variable you may want to look into these SO questions:

Android global variable and Android: How to declare global variables?

Community
  • 1
  • 1
Master Chief
  • 2,520
  • 19
  • 28
0

after you get the imagepath try this, replace package name with you actual package name:

 try {
                BufferedWriter writer = new BufferedWriter(new FileWriter("/data/data/packagename/" + "filename.txt"));

                writer.write(picturePath);

                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

then in your onCreate of the activity with the imageview try this:

ImageButton imagebutton = (ImageButton) findViewById(R.id.imBut);
try {
            BufferedReader inputReader = new BufferedReader(new FileReader("/data/data/packagename/"+ "filename.txt"));
            String inputString;
while ((inputString = inputReader.readLine()) != null) {
Bitmap bitmaporg = BitmapFactory.decodeFile(inputString);
    bitmaporg = Bitmap.createScaledBitmap(bitmaporg,350,350,true);

    imagebutton.setImageBitmap(bitmaporg);
}
        } catch (IOException e) {
            e.printStackTrace();
        }
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • my bad i had picture path still in the while loop decodefile() i edited and put inputString – JRowan Mar 03 '13 at 05:51