【发布时间】:2015-02-15 11:38:46
【问题描述】:
大家好,我正在尝试在我的应用程序中实现通知。问题是,如果可能的话,我希望每个通知都有从 imageview 中获取的特定图像。
由于documentation 表示setSmallIcon() 方法只能将int resId 作为参数,我必须使用setLargeIcon() 方法。如何将来自 URL 的图像转换为位图?
已经尝试过:
Bitmap bmp = BitmapFactory.decodeFile(getIntent().getStringExtra("stockImage"));
builder.setLargeIcon(bmp);
它给了我这个错误:
02-15 11:34:34.576 1615-1615/com.kostas.stockpredictions E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: http:/www.chatapp.info/myProject/images/ALPHA.png: open failed: ENOENT (No such file or directory)
我使用 Ion 库将此 url 设置为 Imageview,如下所示:
iv = (ImageView)findViewById(R.id.currentStockImageViewItem);
Ion.with(iv).placeholder(R.drawable.ic_chat).error(R.drawable.ic_chat).load(i.getStringExtra("stockImage"));
编辑:
class GetBitmapUrl extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
builder.setLargeIcon(result);
}
}
public void getBitmap(){
GetBitmapUrl task = new GetBitmapUrl();
task.execute(getIntent().getStringExtra("stockImage"));
}
我在这里调用这个方法:
Button notify = (Button)findViewById(R.id.buttonNotify);
notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder = new NotificationCompat.Builder(StockItem.this);
builder.setContentTitle(name);
builder.setContentText(getString(R.string.notifyText) + " " + new DecimalFormat("###.##").format(avg));
builder.setWhen(System.currentTimeMillis());
getBitmap();
//builder.setSmallIcon(R.drawable.ic_launcher_stock_custom_icon);
builder.setTicker(getString(R.string.notifyTicker));
builder.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/carme"));
builder.setDefaults(NotificationCompat.DEFAULT_LIGHTS | NotificationCompat.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
Intent intent = new Intent(StockItem.this, ListLoaderActivity.class);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(StockItem.this);
taskStackBuilder.addNextIntent(intent);
taskStackBuilder.addParentStack(ListLoaderActivity.class);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
});
现在通知不显示了..
是否可以使用来自 imageview 的图像的位图?
提前致谢!!!
【问题讨论】:
-
我已经尝试了接受的答案,但它给了我一个错误:
02-15 11:44:49.997 1735-1735/com.kostas.stockpredictions E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.kostas.stockpredictions, PID: 1735 android.os.NetworkOnMainThreadException -
如果你在主流搜索引擎中搜索
NetworkOnMainThreadException,你会得到:stackoverflow.com/questions/6343166/…
标签: android bitmap android-notifications android-bitmap