来自网址的imageview

时间:2012-09-14 18:24:59

标签: android

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

我在链接中使用这种方式。但没有在我的项目中工作。 how to display external image in 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)

2 个答案:

答案 0 :(得分:0)

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

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

所以你可能需要做的是,使用Worker ThreadAsyncTask

答案 1 :(得分:0)

请使用Async Task下载图片,或者您可以使用以下代码解决此问题,在setContentView(R.layout.activity_main);

之后将以下代码写入您的MainActivity文件
if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}