【问题标题】:AsyncTask for loading photo from facebook - always return null用于从 facebook 加载照片的 AsyncTask - 始终返回 null
【发布时间】:2018-08-03 13:27:09
【问题描述】:

我正在使用AsyncTask 从 Facebook 加载照片。 主要问题是bitmapProf(类AsyncTaskLoadPhoto,方法-doInBackground)总是返回null。 我使用调试器和日志搜索错误,但没有找到错误。

这是我的代码:

  private lateinit var imgProfile: ImageView

  override fun onCreate(savedInstanceState: Bundle?) {
    .....
    loadImg()
    }
  }

  fun loadImg() {
    var loadPhoto = AsyncTaskLoadPhoto()
    var bitmap: Bitmap?
    loadPhoto.execute()
    bitmap = loadPhoto.get()
    Log.i("TAG", bitmap.toString())
    imgProfile.setImageBitmap(bitmap)
 }

}

class AsyncTaskLoadPhoto : AsyncTask<Void, Void, Bitmap?>() {

 override fun doInBackground(vararg p0: Void): Bitmap? {
    var bitmapProf: Bitmap? = null
    try {
      val stringURL = "http://graph.facebook.com/"+"userID"+"/picture?width=150&width=150"
      val imgURL = URL(stringURL)
      val connection: HttpURLConnection = imgURL.openConnection() as HttpURLConnection
      connection.doInput = true
      connection.connect()
      val inputStream = connection.inputStream
      bitmapProf = BitmapFactory.decodeStream(inputStream)
    } catch (e: Exception) {
      e.printStackTrace()
     }
    return bitmapProf
 }

【问题讨论】:

  • 我不确定 kotlin 但对于 java 你不能再使用 asynctask 进行网络调用

标签: android android-asynctask kotlin inputstream httpurlconnection


【解决方案1】:
} catch (e: Exception) {
  e.printStackTrace()
 }

您甚至在没有日志语句的情况下就吞下了异常。 e.printStackTrace() 在 Android 中没有可见效果。然后你的方法返回null


作为参考,如果您采用 Kotlin 协程,您的代码将变得如此简单:

class MyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        launch(UI) {
            try {
                imgProfile.setImageBitmap(loadImg())
            } catch (e: Exception) {
                // handle it here
            }
        }
    }

    private suspend fun loadImg(): Bitmap = withContext(CommonPool) {
        val stringURL = "http://graph.facebook.com/userID/picture?width=150&width=150"
        val imgURL = URL(stringURL)
        val connection = imgURL.openConnection() as HttpURLConnection
        connection.connect()
        BitmapFactory.decodeStream(connection.inputStream)
    }
}

这段代码还不干净,你必须检查connection.responseCode,看看输入流是否包含图像数据或只是一些错误消息。

【讨论】:

    【解决方案2】:

    试试这个代码:

    ImageLoader.getInstance().displayImage(imgurl, image_propertypic, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String imageUri, View view) {
    
                            }
    
                            @Override
                            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    
                            }
    
                            @Override
                            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                image_propertypic.setImageBitmap(loadedImage);
                            }
    
                            @Override
                            public void onLoadingCancelled(String imageUri, View view) {
    
                            }
                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      • 2016-07-28
      • 2010-11-08
      相关资源
      最近更新 更多