java - 如何从 url 中提取图像并在 android 中转换为位图

标签 java android

<分区>

我是 Android 的新手,我想发送带有图像的通知。如果我使用可绘制文件夹中的图像意味着我可以做到。但是,我想从 url 中提取图像,然后发送给它……我尝试了一些代码,它使我的应用程序崩溃。怎么做 任何人指导我!

我的代码在这里:

protected static void postNotification(Intent intentAction, Context context,String msg,String url){
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);
    Bitmap bitmap = new ImageDownloaderTask().doInBackground(url);
    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.tapn)
            .setContentTitle("Notification")
            .setContentText(msg)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .getNotification();
    mNotificationManager.notify(R.string.notification_number, notification);
}

ImageDownloaderTask.java:

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private Exception exception;
@Override
public Bitmap doInBackground(String... params) {
    return downloadBitmap(params[0]);
}

private Bitmap downloadBitmap(String url) {
    HttpURLConnection urlConnection = null;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();

        int statusCode = urlConnection.getResponseCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }

        InputStream inputStream = urlConnection.getInputStream();
        if (inputStream != null) {

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
    } catch (Exception e) {
        Log.d("URLCONNECTIONERROR", e.toString());
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        Log.w("ImageDownloader", "Error downloading image from " + url);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();

        }
    }
    return null;
}

protected void onPostExecute(Bitmap feed) {
    // TODO: check this.exception
    // TODO: do something with the feed
}

我的日志:

D/URLCONNECTIONERROR: android.os.NetworkOnMainThreadException

W/ImageDownloader:从 https://www.google.com/intl/en_ALL/images/logo.gif 下载图像时出错

提前致谢!

最佳答案

是的,因为您尝试使用主线程进行联网,以解决您遇到的问题3 解决方案

  1. 使用AsyncTask
  2. 使用回调模式(我非常喜欢这个)
  3. 使用 vinci轻量级安卓库

    • 首先在类上实现RequestpostNotification方法(如this)
    • 两个这样使用库

      Vinci.base(上下文) 。过程() .load(uri, this);

    • 通过onSuccess方法获取位图,就是这样。

      @Override public void onSuccess(位图位图参数){ //位图在这里准备好了 位图变量 = 位图参数; }

关于java - 如何从 url 中提取图像并在 android 中转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137689/

相关文章:

java - JMS 1.1 : Load sharing client side for Point-to-Point mode

android - 如何更改工具栏主页图标颜色

java - 无法通过 Internet 从 PC 服务器接收 Android 上的 UDP 数据

android - 开启硬件加速时的模糊效果

java - 将不断变化的变量的实例存储在java中的另一个变量中

java - 重写通用 equals 方法

Java Apache Poi SXSSF 与 Excel 宏

java - 在 primefaces DataTable 中显示 Hashmap 键和值

android - 将开源项目导入android studio

java - 动态编写 XML