下载并显示图像 [英] Download image and display it
问题描述
应用程序的主要目的是下载和显示图像,但是当我尝试推出应用程序崩溃。
下面是我的code。
私人DownloadImageTask任务;
保护无效的onCreate(包savedInstanceState){
任务=新DownloadImageTask();
task.onPostExecute(task.doInBackground("http://parkcinema.az/uploads/structures/movies/images/xickok_poster1_resized.jpg"));
}
私有类DownloadImageTask扩展的AsyncTask<字符串,太虚,位图> {
受保护的位图doInBackground(字符串...网址){
串urldisplay =网址[0];
位图mIcon11 = NULL;
尝试 {
InputStream的时间=新的java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.de codeStream(中);
}赶上(例外五){
Log.e(错误,e.getMessage());
e.printStackTrace();
}
返回mIcon11;
}
保护无效onPostExecute(位图的结果){
ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
img.setImageBitmap(结果);
}
}
下面是LogCat中:
11月2日至24日:04:56.814:E /跟踪(957):错误打开跟踪文件:没有这样的文件或目录(2)
十一月2日至24日:04:57.384:D / AndroidRuntime(957):关闭虚拟机
十一月2日至24日:04:57.384:W / dalvikvm(957):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a13300)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):致命异常:主要
十一月2日至24日:04:57.404:E / AndroidRuntime(957):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.bakumovies / com.example.bakumovies.MainActivity}:显示java.lang.NullPointerException:的println需要消息
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.os.Handler.dispatchMessage(Handler.java:99)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.os.Looper.loop(Looper.java:137)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread.main(ActivityThread.java:4745)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在java.lang.reflect.Method.invokeNative(本机方法)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在java.lang.reflect.Method.invoke(Method.java:511)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在dalvik.system.NativeStart.main(本机方法)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):由:显示java.lang.NullPointerException:需要的println消息
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.util.Log.println_native(本机方法)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.util.Log.e(Log.java:231)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在com.example.bakumovies.MainActivity $ DownloadImageTask.doInBackground(MainActivity.java:49)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在com.example.bakumovies.MainActivity.onCreate(MainActivity.java:27)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.Activity.performCreate(Activity.java:5008)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
十一月2日至24日:04:57.404:E / AndroidRuntime(957):11 ...更多
这里是的.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
< ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_centerVertical =真
机器人:layout_marginLeft =144dp/>
< / RelativeLayout的>
我不明白为什么它崩溃。图片下载是在新线程启动,ImageView的对象被创建。我完全糊涂了这一点。 任何帮助将AP preciated。
原因NPE(空指针异常)
Log.e(错误,e.getMessage()); //e.getMessage()是空,因此它给NPE。
保护无效的onCreate(包savedInstanceState)
任务=新DownloadImageTask().execute("http://parkcinema.az/uploads/structures/movies/images/xickok_poster1_resized.jpg");
}
受保护的位图doInBackground(字符串...网址){
串urldisplay =网址[0];
位图mIcon11 = NULL;
尝试 {
mIcon11 = BitmapFactory.de codeStream(新的URL(urldisplay).openConnection()的getInputStream());
}
赶上(例外五){
e.printStackTrace();
}
返回mIcon11;
}
添加此权限在AndroidManifest.xml中,因为你正在做的网上操作。
<使用-权限的Android:名称=android.permission.INTERNET对/>
The main purpose of app is to download and display image, but when I try to launch app it crashes.
Here is my code.
private DownloadImageTask task;
protected void onCreate(Bundle savedInstanceState) {
task = new DownloadImageTask();
task.onPostExecute(task.doInBackground("http://parkcinema.az/uploads/structures/movies/images/xickok_poster1_resized.jpg"));
}
private class DownloadImageTask extends AsyncTask <String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(result);
}
}
Here is LogCat:
02-24 11:04:56.814: E/Trace(957): error opening trace file: No such file or directory (2)
02-24 11:04:57.384: D/AndroidRuntime(957): Shutting down VM
02-24 11:04:57.384: W/dalvikvm(957): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-24 11:04:57.404: E/AndroidRuntime(957): FATAL EXCEPTION: main
02-24 11:04:57.404: E/AndroidRuntime(957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bakumovies/com.example.bakumovies.MainActivity}: java.lang.NullPointerException: println needs a message
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.os.Looper.loop(Looper.java:137)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-24 11:04:57.404: E/AndroidRuntime(957): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 11:04:57.404: E/AndroidRuntime(957): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 11:04:57.404: E/AndroidRuntime(957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-24 11:04:57.404: E/AndroidRuntime(957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-24 11:04:57.404: E/AndroidRuntime(957): at dalvik.system.NativeStart.main(Native Method)
02-24 11:04:57.404: E/AndroidRuntime(957): Caused by: java.lang.NullPointerException: println needs a message
02-24 11:04:57.404: E/AndroidRuntime(957): at android.util.Log.println_native(Native Method)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.util.Log.e(Log.java:231)
02-24 11:04:57.404: E/AndroidRuntime(957): at com.example.bakumovies.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:49)
02-24 11:04:57.404: E/AndroidRuntime(957): at com.example.bakumovies.MainActivity.onCreate(MainActivity.java:27)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.Activity.performCreate(Activity.java:5008)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-24 11:04:57.404: E/AndroidRuntime(957): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-24 11:04:57.404: E/AndroidRuntime(957): ... 11 more
And here is .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="144dp" />
</RelativeLayout>
I can't understand why it crashes. Image downloading is launched in new thread, imageview object is created. I am completely confused with this. Any help will be appreciated.
Reason for NPE (Null Pointer Exception)
Log.e("Error", e.getMessage()); //e.getMessage() is NULL so its giving NPE.
protected void onCreate(Bundle savedInstanceState)
task = new DownloadImageTask().execute("http://parkcinema.az/uploads/structures/movies/images/xickok_poster1_resized.jpg");
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
mIcon11 = BitmapFactory.decodeStream(new URL(urldisplay).openConnection().getInputStream());
}
catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
ADD this permission in AndroidManifest.xml because you are doing internet operation.
<uses-permission android:name="android.permission.INTERNET" />
这篇关于下载并显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!