我正在尝试使用AsyncTask通过互联网提取图像并显示在RecyclerViewer上,但是我收到以下错误消息,该消息是从我在我的doInBackground中的Exception从RecyclerViewAdapter类中捕获的:
08-02 00:11:25.608: E/ImageDownload(5753): Download failed: Permission denied (missing INTERNET permission?)
请参阅下面的完整版AsyncTask子类:
class GetImageFromNet extends AsyncTask<String, Void, Bitmap> {
private RVAdapter.PersonViewHolder personViewHolder;
private String url = "http://ia.media-imdb.com/images/M/MV5BNDMyODU3ODk3Ml5BMl5BanBnXkFtZTgwNDc1ODkwNjE@._V1_SX300.jpg";
public GetImageFromNet(RVAdapter.PersonViewHolder personViewHolder){
this.personViewHolder = personViewHolder;
}
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
personViewHolder.personPhoto.setImageBitmap(bitmap);
}
由于我已在AndroidManifest.xml中添加了以下权限访问权限,因此我不知道为什么会出现上述错误:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮忙!
答案 0 :(得分:1)
将<uses-permission>
元素移动为<manifest>
的直接子元素,高于<application>
元素。
答案 1 :(得分:1)
将您的清单更新为: -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>