想用SD卡上的图片做Activity背景 我想使用SD卡上的指定文件作为Activity和其它控件的背景,这样的话,我只需要更换图片就可以换背景了,要实现这个功能,请问一手该怎么做?我看到的方法,都是需要把图片放到程序的资源库中,这样一来程序会变得很大,二来不能更换,故请教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以先把文件转化为字节数组,再转为bitmap,再转为BitmapDrawable,此类是Drawable的子类可以直接设为背景写了个一段小代码,LZ可以参考下try { LinearLayout ll = (LinearLayout)this.findViewById(R.id.line); File file = new File(Environment.getExternalStorageDirectory()+"/device.png"); InputStream inputStream = new FileInputStream(file); long length = file.length(); byte[] bytes = new byte[(int)length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead= inputStream.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); BitmapDrawable bd= new BitmapDrawable(this.getResources(), bitmap); ll.setBackgroundDrawable(bd); } catch (Exception e) { e.printStackTrace(); } 呵呵,回楼上,谢谢了,俺已经搞定了:)不过还是非常感谢:)代码如下:Bitmap bm = BitmapFactory.decodeFile("/sdcard/title.jpg", new Options());BitmapDrawable bd = new BitmapDrawable(bm);ly.setBackgroundDrawable(bd); 如何在android上查看,编辑word,ppt,excel? 出了个很小的状况,一直都在prepare 是否建议方法里加final GridView中Focus的问题 如何判断手机先用网络是否连接至 服务器。 话题:能否利用NDK把第三方so文件打入apk中 eclipse 连接真机调试 android实现下载文件功能,在保存文件的时候报Permission denied 用Jsoup抓取数据,如何设置适配手机网络也能抓取 android用qq第三方登录出错怎么办 关于调用Android自带图片浏览器的问题 android城市名称获取经度和纬度
写了个一段小代码,LZ可以参考下try {
LinearLayout ll = (LinearLayout)this.findViewById(R.id.line);
File file = new File(Environment.getExternalStorageDirectory()+"/device.png");
InputStream inputStream = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length &&
(numRead= inputStream.read(bytes, offset, bytes.length-offset)) >= 0)
{
offset += numRead;
}
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
BitmapDrawable bd= new BitmapDrawable(this.getResources(), bitmap);
ll.setBackgroundDrawable(bd);
} catch (Exception e) {
e.printStackTrace();
}
不过还是非常感谢:)
代码如下:
Bitmap bm = BitmapFactory.decodeFile("/sdcard/title.jpg", new Options());
BitmapDrawable bd = new BitmapDrawable(bm);
ly.setBackgroundDrawable(bd);