0

guy

I want to change the background of an activity with an image selected from the gallery

i use the following to set the background using an image from the resources folder

View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.phonebackground);

this sets the image "Phonebackground" from the drawable folder to the background

I have a method that allows me to pick a photo from the gallery which is as follows

Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();

this gives the file in the format of

content://media/external/images/media/1698

how do i use this data in the targetUri to set the background

Any help appreciated

Mark

EDIT:

ENTIRE CODE

View background = findViewById(R.id.background);
    String fileUrl = "background.txt";
    String file = android.os.Environment.getExternalStorageDirectory().getPath() +    fileUrl;
    File f = getBaseContext().getFileStreamPath(fileUrl);
    if(f.exists())
         try{
        FileInputStream fin = openFileInput(fileUrl);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                temp = temp + Character.toString((char)c);
             }
              String asubstring = temp.substring(0, 1); 
             if(asubstring.equals("/")) 
             Drawable myDrawable = new BitmapDrawable(getResources(), temp);
             ImageView backgroundView = (ImageView) findViewById(R.id.background);
             backgroundView.setImageURI(null); 
             backgroundView.setImageDrawable(myDrawable);

             if(temp.equals("healthblue"))
             background.setBackgroundResource(R.drawable.healthblack);
             if(temp.equals("justice"))
                 background.setBackgroundResource(R.drawable.justiceblack);
             if(temp.equals("tech"))
                 background.setBackgroundResource(R.drawable.phonebackground);
             if(temp.equals("raynesback"))
                 background.setBackgroundResource(R.drawable.raynesback);
             if(temp.equals("ebbs"))
                 background.setBackgroundResource(R.drawable.ebbs);
             if(temp.equals("defenceblack"))
                 background.setBackgroundResource(R.drawable.defenceblack);
             if(temp.equals("corporate"))
                 background.setBackgroundResource(R.drawable.corporate);
             if(temp.equals("remoteblack"))
                 background.setBackgroundResource(R.drawable.remoteblack);
             if(temp.equals("prestigeblack"))
                 background.setBackgroundResource(R.drawable.prestigeblack);

             }catch(Exception e){
          }
    else{
        Toast.makeText(getBaseContext(),"NOT There",Toast.LENGTH_SHORT).show();
    }

Trying to set the background from the path got from a text file after the line

if(asubstring.equals("/"))

Any help appreciated

Mark

user3422687
  • 229
  • 1
  • 8
  • 27
  • I believe this is what you are looking for. http://stackoverflow.com/questions/16718257/retrive-drawable-resource-from-uri – Jay Snayder Jul 22 '14 at 20:14

2 Answers2

1

You can try this way:

1) Open gallery

private void selectImageFromGallery() {
    final Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
    intent.setType( "image/*" );
    this.startActivityForResult( intent, 0 );

}

2) Getting uri and creating a drawable

@Override
public void onActivityResult( final int requestCode, final int resultCode, final Intent data ) {
    super.onActivityResult( requestCode, resultCode, data );

    if ( resultCode == Activity.RESULT_OK ) {
        final Uri targetUri = data.getData();

        InputStream is;
        try {
            is = this.getContentResolver().openInputStream( targetUri );
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 10;
            Bitmap bitmap=BitmapFactory.decodeStream(is,null,options);

            Drawable background = new BitmapDrawable(getResources(),bitmap);

            view.setBackground(background);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }            

    }

}   
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
wbelarmino
  • 509
  • 3
  • 7
  • once I added private Drawable mDrawable; there were no errors but once i pick the picture it crashes the app. Does it make a difference that the code to set this is in Activity2.java but the background I am setting is on activity1.java – user3422687 Jul 22 '14 at 22:46
  • What is the crash log? – wbelarmino Jul 22 '14 at 23:21
  • soryy im pretty much a newbie to this how do i get that from the phone. I cant run it on the emulator as it cant access an sd card on that so i cant see where its crashing – user3422687 Jul 23 '14 at 05:14
  • Sorry i not got it what's your problem. Can you give me more details? – wbelarmino Jul 23 '14 at 15:00
  • I stopped it from crashing but cant get it to work I have the path to the picture in a string called "temp" How can I set the background of a layout called "background1" to the picture on the sd card. the file path takes the form "/storage/emulated/0/Pictures/pic1.jpg" – user3422687 Jul 26 '14 at 22:13
0

thanks for the help the following works

View background = findViewById(R.id.background);
    String fileUrl = "background.txt";
    String file3 = android.os.Environment.getExternalStorageDirectory().getPath(); 
    File f = getBaseContext().getFileStreamPath(fileUrl);
    if(f.exists())
         try{
        FileInputStream fin = openFileInput(fileUrl);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                temp = temp + Character.toString((char)c);
             }
            String asubstring = temp.substring(0, 1); 
            if(asubstring.equals("/")) ;
            Resources res = getResources();
            Bitmap bitmap = BitmapFactory.decodeFile(temp);
            BitmapDrawable bd = new BitmapDrawable(res, bitmap);
            View view = findViewById(R.id.background);
            view.setBackgroundDrawable(bd);

             if(temp.equals("healthblue"))
             background.setBackgroundResource(R.drawable.healthblack);
             if(temp.equals("justice"))
                 background.setBackgroundResource(R.drawable.justiceblack);
             if(temp.equals("tech"))
                 background.setBackgroundResource(R.drawable.phonebackground);
             if(temp.equals("raynesback"))
                 background.setBackgroundResource(R.drawable.raynesback);
             if(temp.equals("ebbs"))
                 background.setBackgroundResource(R.drawable.ebbs);
             if(temp.equals("defenceblack"))
                 background.setBackgroundResource(R.drawable.defenceblack);
             if(temp.equals("corporate"))
                 background.setBackgroundResource(R.drawable.corporate);
             if(temp.equals("remoteblack"))
                 background.setBackgroundResource(R.drawable.remoteblack);
             if(temp.equals("prestigeblack"))
                 background.setBackgroundResource(R.drawable.prestigeblack);

             }catch(Exception e){
          }
    else{
        Toast.makeText(getBaseContext(),"NOT There",Toast.LENGTH_SHORT).show();
    }

Mark

user3422687
  • 229
  • 1
  • 8
  • 27