无法从网址上传图片

时间:2014-12-28 20:01:20

标签: java android

我创建Android应用程序并希望从URL上传图像到ImageView。 在Android清单中设置权限

<uses-permission android:name="android.permission.INTERNET"/>

尝试从网址上传图片

public void getBitmapfromURL( String src )
{
  try
  {
    URL url = new URL( src );
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = url.openStream();
    Bitmap bmp = BitmapFactory.decodeStream(input);

    return bmp;
  }
  catch( Exception ex )
  {
    Log.i("MyMessage", "ERROR!!!!");

    return null;
  }
}

我尝试从网址上传图片时的堆栈跟踪

12-28 22:36:33.501: W/System.err(4605): android.os.NetworkOnMainThreadException
12-28 22:36:33.501: W/System.err(4605): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-28 22:36:33.501: W/System.err(4605): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-28 22:36:33.501: W/System.err(4605): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-28 22:36:33.501: W/System.err(4605): 	at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-28 22:36:33.501: W/System.err(4605): 	at com.example.testloadimg.MainActivity.getBitmapfromURL(MainActivity.java:35)
12-28 22:36:33.501: W/System.err(4605): 	at com.example.testloadimg.MainActivity.onCreate(MainActivity.java:25)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.Activity.performCreate(Activity.java:5231)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-28 22:36:33.501: W/System.err(4605): 	at android.os.Handler.dispatchMessage(Handler.java:102)
12-28 22:36:33.501: W/System.err(4605): 	at android.os.Looper.loop(Looper.java:136)
12-28 22:36:33.501: W/System.err(4605): 	at android.app.ActivityThread.main(ActivityThread.java:5001)
12-28 22:36:33.501: W/System.err(4605): 	at java.lang.reflect.Method.invokeNative(Native Method)
12-28 22:36:33.501: W/System.err(4605): 	at java.lang.reflect.Method.invoke(Method.java:515)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-28 22:36:33.501: W/System.err(4605): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-28 22:36:33.501: W/System.err(4605): 	at dalvik.system.NativeStart.main(Native Method)
12-28 22:36:33.501: E/MyMessage(4605): ERROR!!!!
12-28 22:36:33.685: D/libEGL(4605): loaded /system/lib/egl/libEGL_genymotion.so
12-28 22:36:33.693: D/(4605): HostConnection::get() New Host Connection established 0xb7dd9ab0, tid 4605
12-28 22:36:33.717: D/libEGL(4605): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-28 22:36:33.717: D/libEGL(4605): loaded /system/lib/egl/libGLESv2_genymotion.so
12-28 22:36:33.901: W/EGL_genymotion(4605): eglSurfaceAttrib not implemented
12-28 22:36:33.909: E/OpenGLRenderer(4605): Getting MAX_TEXTURE_SIZE from GradienCache
12-28 22:36:33.913: E/OpenGLRenderer(4605): MAX_TEXTURE_SIZE: 16384
12-28 22:36:33.937: E/OpenGLRenderer(4605): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
12-28 22:36:33.941: E/OpenGLRenderer(4605): MAX_TEXTURE_SIZE: 16384
12-28 22:36:33.941: D/OpenGLRenderer(4605): Enabling debug mode 0

2 个答案:

答案 0 :(得分:0)

这一行你的问题非常清楚:

12-28 22:36:33.501: W/System.err(4605): android.os.NetworkOnMainThreadException

这意味着您应该移动与网络连接相关的代码,并将它们放在一个单独的线程中。

请查看Android Developers

中的AsyncTask部分

答案 1 :(得分:0)

自从3.0以来,你无法在Strict模式下在UI线程上运行网络代码,你可以覆盖这个,这是一种不好的做法:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

或创建一个AsyncTask - 例如一个内部类 - 它将加载图像:

private Bitmap myBitmapFromURL;
private class ImageLoadTask extends AsyncTask<String, Void, Bitmap> {

private ImageView imageView;    

@Override
protected Bitmap doInBackground(String... params) {
    try {
        String src = params[0];
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(input);
        return bmp;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);
    myBitmapFromURL = result;
}
}

然后运行任务:

new ImageLoadTask().execute("URL of the file");