-3

我使用来自 url 的多种外部 imageview 方式。但不幸的是,所有的时间活动都停止了。你知道这就是为什么。我在清单文件中有互联网限制。但我无法解决。有谁可以帮助我。?

我在链接中使用这种方式。但不在我的项目中工作。 如何在android中显示外部图像?

        public class MainActivity extends Activity{
            ImageView imgView;
            EditText inputUrl;
            OnClickListener getImageBtnOnClick = new OnClickListener() {
                public void onClick(View view) {
                    Context context = view.getContext();
                    Editable ed = inputUrl.getText();
                    Drawable image = ImageOperations(context,"http://www.cssnz.org/flower.jpg" ,"image.jpg");

                    imgView = (ImageView)findViewById(R.id.i1);
                    imgView.setImageDrawable(image);
                }
            };

            public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                setContentView(R.layout.activity_main);
                inputUrl = ((EditText)findViewById(R.id.imageURL));
                inputUrl.setSingleLine();
                inputUrl.setTextSize(11);
                Button getImageButton = (Button)findViewById(R.id.getImageButton);
                getImageButton.setOnClickListener(getImageBtnOnClick);

            }   

            private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
                try {
                    InputStream is = (InputStream) this.fetch(url);
                    Drawable d = Drawable.createFromStream(is, "src");
                    return d;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            public Object fetch(String address) throws MalformedURLException,IOException {
                URL url = new URL(address);
                Object content = url.getContent();
                return content;
            }
        }
    xml file
    <LinearLayout 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" >

        <Button
            android:id="@+id/getImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="39dp"
            android:layout_marginTop="22dp" />

        <ImageView
            android:id="@+id/i1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="106dp"
            android:src="@drawable/ic_action_search" />

        <EditText
            android:id="@+id/imageURL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>


    android.manifest  

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.externalimageview"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

log cat
09-14 19:56:31.409: W/dalvikvm(1097): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-14 19:56:31.459: E/AndroidRuntime(1097): FATAL EXCEPTION: main
09-14 19:56:31.459: E/AndroidRuntime(1097): android.os.NetworkOnMainThreadException
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.URLConnection.getContent(URLConnection.java:194)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.net.URL.getContent(URL.java:447)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.fetch(MainActivity.java:61)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.ImageOperations(MainActivity.java:47)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity.access$0(MainActivity.java:45)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.example.externalimageview.MainActivity$1.onClick(MainActivity.java:27)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.view.View.performClick(View.java:3511)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.view.View$PerformClick.run(View.java:14105)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Handler.handleCallback(Handler.java:605)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.os.Looper.loop(Looper.java:137)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at java.lang.reflect.Method.invoke(Method.java:511)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-14 19:56:31.459: E/AndroidRuntime(1097):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

你正面临着android.os.NetworkOnMainThreadException。新引入了此异常,以限制您的应用程序从主 UI 线程访问网络。

这样做的主要用途是避免您的应用停止响应用户交互。当从主线程访问网络时,它实际上会阻止 UI 响应,直到方法完成执行。所以为了避免这个android抛出这个异常。

因此,您可能需要做的是,使用 aWorker ThreadAsyncTask.

于 2012-09-15T06:35:23.283 回答
0

请使用异步任务下载图像,或者您可以使用下面的代码来解决这个问题,之后将下面的代码写入您的 MainActivity 文件setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
于 2012-09-15T06:53:24.413 回答