Java 无法从URL加载位图以在通知中显示-Android
我正在试着加载 在Android中转换为位图。我使用AsyncTask在后台执行下载,在onPostExecute中生成通知。将显示通知,但不显示图像 问题是返回的位图为空。网络调用中似乎没有错误,因为输入流和连接不为null NotificationUtils中的方法:Java 无法从URL加载位图以在通知中显示-Android,java,android,image,url,bitmap,Java,Android,Image,Url,Bitmap,我正在试着加载 在Android中转换为位图。我使用AsyncTask在后台执行下载,在onPostExecute中生成通知。将显示通知,但不显示图像 问题是返回的位图为空。网络调用中似乎没有错误,因为输入流和连接不为null NotificationUtils中的方法: public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent inte
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
if (!TextUtils.isEmpty(imageUrl)) {
if (imageUrl != null && imageUrl.length() > 4 && Patterns.WEB_URL.matcher(imageUrl).matches()) {
/*ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});*/
/*Bitmap test = null;
try {
test = Picasso.with(mContext).load(imageUrl).get();
} catch (IOException e) {
e.printStackTrace();
}*/
//Bitmap bitmap = getBitmapFromURL(imageUrl);
/*ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});*/
//Bitmap bitmap1 = imageLoader.loadImageSync(imageUrl);
//Bitmap mbitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.verifiedicon);
new AsyncTask<String, String, Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(input);
input.close();
return bmp;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
showBigNotification(bitmap, mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
} else {
showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
Toast.makeText(mContext, "Bitmap is null", Toast.LENGTH_SHORT).show();
}
}
}.execute(imageUrl);
}
} else {
showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
playNotificationSound();
}
}
我已经尝试过UniversalImageLoader,如下所示:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
Log.i("ayusch",loadedImage.toString());
}
});
当我使用它时,通知根本不会显示
我试着用毕加索来做:
Bitmap test = Picasso.with(mContext).load(imageUrl).get();
但同样,通知未显示
请帮助此问题已解决我已尝试此操作,但未成功…..(您正在尝试哪个版本的Android?minsdkversion是15此问题已解决我已尝试此操作,但未成功…..(您正在尝试哪个版本的Android?minsdkversion是15)
Bitmap test = Picasso.with(mContext).load(imageUrl).get();