androidj基础:从网上下载图片

一、布局文件

设置界面,添加一个ImageView,和两个Button按钮,设置其属性及id

 <ImageView
       android:id="@+id/ImageView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:background="#ffffff"

       />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    >
   <Button
       android:id="@+id/bt1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="下载图片"
       />

   <Button
       android:id="@+id/bt2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:layout_toRightOf="@+id/bt1"

       android:text="读取图片"
       />

二、声明网络权限

因为使用网络功能需要添加权限,修改AndroidManifest.xml文件添加权限声明语句。

 <uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.httpexe">
    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

三、编辑ManActivity.java文件。

import...//import语句自动生成,略
public
class MainActivity extends AppCompatActivity { ImageView imageView; Button button; Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.ImageView); button = (Button) findViewById(R.id.bt1); button1=(Button)findViewById(R.id.bt2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyTask task = new MyTask();//创建异步任务对象 task.execute("https://www.baidu.com/img/bd_logo1.png");//创建下载任务 } }); button1.setOnClickListener(new View.OnClickListener() {//读取图片 @Override public void onClick(View v) { Bitmap bmp=null; try{ FileInputStream fis= openFileInput("a.png"); bmp=BitmapFactory.decodeStream(fis); if(bmp!=null) { imageView.setImageBitmap(bmp); } }catch (Exception ex) { } } }); } public class MyTask extends AsyncTask<String, Void, Bitmap> { //bmp位图,泛型 @Override protected Bitmap doInBackground(String... params) { //真正的下载 String urlStr = params[0]; //取第一个变参 Bitmap bmp = null; try { URL url = new URL(urlStr); bmp=BitmapFactory.decodeStream(url.openStream()); //位图工厂,返回bitmap InputStream is= url.openStream(); byte[] buffer=new byte[4096];//byte[]缓冲区 FileOutputStream fos= MainActivity.this.openFileOutput("a.png",MainActivity.MODE_PRIVATE); int hasRead=is.read(buffer); while (hasRead>0){ fos.write(buffer,0,hasRead);//文件大小,起始位置,读取的量 hasRead=is.read(buffer); } fos.close();//关闭 is.close();//关闭 } catch (Exception ex) { }finally { } return bmp; } @Override protected void onPostExecute(Bitmap bitmap) { if(bitmap!=null){ imageView.setImageBitmap(bitmap); } } }

 

 四、运行结果

点击下载按钮下载图片。读取图片按钮的作用:下载一次后,不需要重新下载

小记:

java代码输入时有些会自动弹出,并匹配到相应包,直接贴会有报错。

 

posted @ 2017-05-07 13:47  第101次弃游  Views(714)  Comments(0Edit  收藏  举报