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