Android 从网站崩溃加载图像
可能重复:Android 从网站崩溃加载图像,android,Android,可能重复: 我试图从一个网站加载一个图像,我看到了这个问题:但我的应用程序在这行崩溃了: 连接() main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" an
我试图从一个网站加载一个图像,我看到了这个问题:但我的应用程序在这行崩溃了: 连接() main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HTTPImage load test"
/>
<Button
android:id="@+id/get_imagebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get an image"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
我的舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtest.HTTPTest"
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>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- Permission to write to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
提前感谢您的帮助
日尔曼
但我的应用程序在这一行崩溃:conn.connect()代码>
从Android 3+开始,您无法执行主线程的潜在慢速网络操作
在异步任务中下载映像,这样就不会降低用户体验。只需将downloadFile()
方法移动到异步任务的doInBackground()
方法中
如果您需要详细的细节,有一个解决方案
添加
使用Jimbo的doInBackground()
尝试从其他线程访问UI时可能会出现错误,而这是无法做到的。您可以在AsyncTask中重写onPostExecute()
,以处理视图,因为它可以访问UI线程:
@Override
protected void onPostExecute(String unused) {
imView.setImageBitmap(bmImg);
}
但我的应用程序在这一行崩溃:conn.connect()代码>
从Android 3+开始,您无法执行主线程的潜在慢速网络操作
在异步任务中下载映像,这样就不会降低用户体验。只需将downloadFile()
方法移动到异步任务的doInBackground()
方法中
如果您需要详细的细节,有一个解决方案
添加
使用Jimbo的doInBackground()
尝试从其他线程访问UI时可能会出现错误,而这是无法做到的。您可以在AsyncTask中重写onPostExecute()
,以处理视图,因为它可以访问UI线程:
@Override
protected void onPostExecute(String unused) {
imView.setImageBitmap(bmImg);
}
您应该在另一个线程(即不在UI线程)上运行所有网络请求。事实上,Android让你这么做。否则,在等待响应时,UI将锁定。将downloadFile()方法更改为如下所示。异步任务将在另一个线程上运行代码。对于需要一段时间才能执行的任务,这是一个很好的实践
void downloadFile(String fileUrl) {
AsyncTask<String, Object, String> task = new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... params) {
URL myFileUrl = null;
try {
myFileUrl = new URL(params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute(fileUrl);
}
void下载文件(字符串文件URL){
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护的字符串doInBackground(字符串…参数){
URL myFileUrl=null;
试一试{
myFileUrl=新URL(参数[0]);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
HttpURLConnection conn=(HttpURLConnection)myFileUrl
.openConnection();
conn.setDoInput(真);
连接();
InputStream is=conn.getInputStream();
bmImg=BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
};
task.execute(fileUrl);
}
您应该在另一个线程(即不在UI线程)上运行所有网络请求。事实上,Android让你这么做。否则,在等待响应时,UI将锁定。将downloadFile()方法更改为如下所示。异步任务将在另一个线程上运行代码。对于需要一段时间才能执行的任务,这是一个很好的实践
void downloadFile(String fileUrl) {
AsyncTask<String, Object, String> task = new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... params) {
URL myFileUrl = null;
try {
myFileUrl = new URL(params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
task.execute(fileUrl);
}
void下载文件(字符串文件URL){
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护的字符串doInBackground(字符串…参数){
URL myFileUrl=null;
试一试{
myFileUrl=新URL(参数[0]);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
HttpURLConnection conn=(HttpURLConnection)myFileUrl
.openConnection();
conn.setDoInput(真);
连接();
InputStream is=conn.getInputStream();
bmImg=BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
};
task.execute(fileUrl);
}
我只能猜测NetworkOnMainThread是个例外……哦,是的。我怎样才能解决这个问题?我的catlog:11-19 15:12:52.655:E/AndroidRuntime(21823):致命异常:main 11-19 15:12:52.655:E/AndroidRuntime(21823):android.os.NetworkMainThreadException 11-19 15:12:52.655:E/AndroidRuntime(21823):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)…将来,每当应用程序崩溃时,总是将您的日志添加到您的问题中,这是帮助我们解决您的错误的路线图。好的,我将感谢您的回答,我现在正在查看您的链接/url我为您的新错误发布了解决方案。我只能猜测例外是NetworkOnMainThread…哦,是的。我怎样才能解决这个问题?我的catlog:11-19 15:12:52.655:E/AndroidRuntime(21823):致命异常:main 11-19 15:12:52.655:E/AndroidRuntime(21823):android.os.NetworkMainThreadException 11-19 15:12:52.655:E/AndroidRuntime(21823):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)…将来,每当应用程序崩溃时,请始终将您的日志添加到您的问题中,这是帮助我们解决您的错误的路线图。好的,我将感谢您的回答,我现在正在查看您的链接/url。我为您的新错误发布了解决方案。您只能运行一次AsyncTask的实例。“任务只能执行一次(如果