开发者社区> 问答> 正文

关于网络权限,访问网络图片

下面是使用到的三个文件:
为什么显示不了图片呢?

 AndroidManifest.xml:
 <?xml version="1.0" encoding="utf-8"?>
 package="com.imooc.summenudemo"
 android:versionCode="1"
 android:versionName="1.0" >
 android:minSdkVersion="8"
 android:targetSdkVersion="18" />

 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 android:name="com.imooc.summenudemo.MainActivity"
 android:label="@string/app_name"
 android:theme="@android:style/Theme.Light.NoTitleBar"
 >
 android:name="com.imooc.summenudemo.ImageActivity"
 android:label="@string/app_name"
 android:theme="@android:style/Theme.Light.NoTitleBar"
 >


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



image_view.xml
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="显示" />
<ImageView
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"
    android:id="@+id/image" />  




ImageActivity.java
 package com.imooc.summenudemo;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import android.os.Bundle;
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.widget.ImageView;

public class ImageActivity extends Activity {
 /** Called when the activity is first created. */

 String imageUrl = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";

 Bitmap bmImg;

 ImageView imView;

 @Override

 public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 imView = (ImageView) findViewById(R.id.image);

 imView.setImageBitmap(returnBitMap(imageUrl));

 }

 public Bitmap returnBitMap(String url){

 URL myFileUrl = null;

 Bitmap bitmap = null;

 try {

 myFileUrl = new URL(url);

 } catch (MalformedURLException e) {

 e.printStackTrace();

 }

 try {

 HttpURLConnection conn = (HttpURLConnection) myFileUrl

 .openConnection();

 conn.setDoInput(true);

 conn.connect();

 InputStream is = conn.getInputStream();

 bitmap = BitmapFactory.decodeStream(is);

 is.close();

 } catch (IOException e) {

 e.printStackTrace();

 }

 return bitmap;

 }

 }

展开
收起
爵霸 2016-03-20 11:00:48 2465 0
1 条回答
写回答
取消 提交回答
  • 
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="显示" />
    <ImageView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:id="@+id/image" />  
    
    2019-07-17 19:09:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
可编程网络视角的网络创新研究 立即下载
可编程网络和SDN3.0 立即下载
开放促进创新:构建开放网络生态 立即下载