我想使用SD卡上的指定文件作为Activity和其它控件的背景,这样的话,我只需要更换图片就可以换背景了,要实现这个功能,请问一手该怎么做?我看到的方法,都是需要把图片放到程序的资源库中,这样一来程序会变得很大,二来不能更换,故请教

解决方案 »

  1.   

    可以先把文件转化为字节数组,再转为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();
    }
      

  2.   

    呵呵,回楼上,谢谢了,俺已经搞定了:)
    不过还是非常感谢:)
    代码如下:
    Bitmap bm = BitmapFactory.decodeFile("/sdcard/title.jpg", new Options());
    BitmapDrawable bd = new BitmapDrawable(bm);
    ly.setBackgroundDrawable(bd);