【发布时间】:2014-07-22 20:05:22
【问题描述】:
伙计
我想用从图库中选择的图像更改活动的背景
我使用资源文件夹中的图像来设置背景
View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.phonebackground);
这会将可绘制文件夹中的图像“Phonebackground”设置为背景
我有一个方法可以让我从图库中挑选一张照片,如下所示
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();
这给出了格式为
的文件content://media/external/images/media/1698
如何使用targetUri中的这些数据来设置背景
任何帮助表示赞赏
标记
编辑:
整个代码
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();
}
尝试从从文本文件中获得的路径设置背景
if(asubstring.equals("/"))
任何帮助表示赞赏
标记
【问题讨论】:
-
我相信这就是你要找的。 stackoverflow.com/questions/16718257/…
标签: android background set gallery