R.drawable.bgimg2应用的是工程drawable中的资源,这是android的一个资源映射,你要现实sd卡中的图片,只能讲图片加载为drawabel然后通过setBackgroundDrawable来实现
/**
* 生成图片
*
* @param path
* @return
*/
public static Bitmap creatBitmap(String path) {
if (path == null || path.equals("")) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
try {
Bitmap bitmap = BitmapFactory.decodeFile(path);
RocLog.info("ViewUtil", "@@creatBitmap@@ the is bitmap :" + bitmap);
return bitmap;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}setBackgroundDrawable(new BitmapDrawable(creatBitmap(path)));
/**
* 生成图片
*
* @param path
* @return
*/
public static Bitmap creatBitmap(String path) {
if (path == null || path.equals("")) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
try {
Bitmap bitmap = BitmapFactory.decodeFile(path);
RocLog.info("ViewUtil", "@@creatBitmap@@ the is bitmap :" + bitmap);
return bitmap;
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}setBackgroundDrawable(new BitmapDrawable(creatBitmap(path)));
解决方案 »
- android ListView 实现表格如何获取到每个单元格内的值???
- 新手求助,添加XML程序报错
- android 按Home切回到程序后都做了什么?
- android:screenOrientation="landscape" 程序跑两遍
- 能不能在mediaPlayer.setOnCompletionListener中release 这个mediaPlayer对象自身
- Dijstra
- android4.0 外接USB摄像头 有没有成功的先例
- webview加载的网页,js调用问题,高手来指点一下
- 能够实现图片的双指放大缩小,双击放大缩小,类似系统图库应用,有现成的控件可用么?
- 如何自定义JieCaoVedio的UI?
- android 多个SD卡检测
- jsoup 的问题----在线等啊
或者用Service和广播都可以实现
简单写了一个例子。。
点击btn1实现改背景的效果package com.lxl.testapp;import java.io.IOException;
import java.io.InputStream;
import java.util.List;import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;public class MainActivity extends Activity {
Context context;
private NotificationManager mNotificationManager; //通知管理
int NOTIFICATIONS_ID=111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context=this;
Log.e("TEST", "MainActivity启动");
setContentView(R.layout.activity_main);
// ImageView i=new ImageView(this);
// i.setBackgroundResource(R.drawable.showicon);
// WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
// wm.addView(i, new WindowManager.LayoutParams(-1,-1));
List<PackageInfo> packs = context.getPackageManager()
.getInstalledPackages(0);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //初始化管理器
final RelativeLayout r=(RelativeLayout) findViewById(R.id.layout);
final WaitDialog waitDialog = new WaitDialog(context);
Button btn1=(Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputStream open=null;
try {
open = context.getResources().getAssets().open("showicon.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(open, "showicon.png");
r.setBackground(d);
}
});
Button btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClass(context, MyActivity.class);
context.startActivity(i);
}
});
Button btn3=(Button)findViewById(R.id.btn3);
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setWeather("信息", "标题", "内容", R.drawable.showicon);
}
});
}
@Override
protected void onDestroy() {
Log.e("TEST", "MainActivity结束");
super.onDestroy();
} private void setWeather(String tickerText, String title, String content,
int drawable) {
// 建立一个通知实例,第一个参数是图片,第二个标题栏上显示的文字,第三个是时间
Notification noti = new Notification(drawable, tickerText,
System.currentTimeMillis());
// 当单击下拉下来的标题内容时候做什么,这里是跳转到主界面。这里和下面是一起的。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(), 0);
// 设置样式,默认为声音
noti.defaults = Notification.DEFAULT_SOUND;
// Title 是拉下来的标题,Content也是下拉后的内容显示
noti.setLatestEventInfo(this, title, content, contentIntent);
// 显示这个通知
mNotificationManager.notify(NOTIFICATIONS_ID, noti);
}}