人
我想用从图库
中选择的图像更改活动的背景我使用以下内容使用资源文件夹
中的图像设置背景View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.phonebackground);
这将设置图像" Phonebackground"从drawable文件夹到背景
我有一种方法可以让我从画廊中选择一张照片,如下所示
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();
这将以
的格式提供文件内容://媒体/外部/图像/媒体/ 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(" /"))
任何帮助表示赞赏
标记
答案 0 :(得分:1)
您可以尝试这种方式:
1)打开图库
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)获取uri并创建一个可绘制的
@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();
}
}
}
答案 1 :(得分:0)
感谢以下工作的帮助
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();
}
标记