android实现图片下载到sdcard中的例子,用进度条显示现在进度,同时间图片显示出来...

news/2024/7/8 12:47:47

功能简介:从网络中下载一个图片,图片的格式为指定的格式,将图片保存在SDcard的根目录中,同时显示出来。

源代码:

代码需要进一步改进,忘高手指点 package com.acer.download; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class Download extends Activity { /** Called when the activity is first created. */ private EditText url; private Button download; private ProgressBar progress; private String url_address; private Bitmap bitmap=null; private FrameLayout frameLayout; private int file_length; private int downloadfilesize=0; private String downloadPath=Environment.getExternalStorageDirectory().getAbsolutePath(); private String Url_address; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListener(); } //get the widget private void findViews(){ url=(EditText)this.findViewById(R.id.url); download=(Button)this.findViewById(R.id.download); frameLayout=(FrameLayout)this.findViewById(R.id.frameLayout); progress=(ProgressBar)this.findViewById(R.id.progress); progress.setVisibility(View.GONE); progress.incrementProgressBy(1); } //handle the users action private void setListener(){ download.setOnClickListener(download_start); } //send the message to hander function private void sendMsg(int flag) { Message msg = new Message(); msg.what = flag; handler.sendMessage(msg); } //view the picture private Handler handler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what) { case 0: progress.setMax(file_length); break; case 1: progress.setProgress(downloadfilesize); break; case 2: //view the picture which read from the sdcard String my_image_path="/sdcard/image.jpg"; BitmapFactory.Options options=new BitmapFactory.Options(); bitmap=BitmapFactory.decodeFile(my_image_path,options); ImageView view=(ImageView)frameLayout.findViewById(R.id.image); view.setImageBitmap(bitmap); progress.setVisibility(view.GONE); //dismiss the progressBar break; } super.handleMessage(msg); } }; // the function is to test the URL is ends with the given Strings or not private boolean formatValid(String str){ String str1; str1=str.toLowerCase(); if(str1.endsWith(".png")||str1.endsWith(".jpg")||str1.endsWith(".gif")||str1.endsWith(".bmp")) return true; return false; } //handle the button's click public OnClickListener download_start=new Button.OnClickListener(){ public void onClick(final View view){ progress.setVisibility(view.VISIBLE); //view the progressBar progress.setProgress(0); //test the HTTP_URL is right or not String myUrl=url.getText().toString(); if(formatValid(myUrl)) { Url_address=myUrl; } else { Toast.makeText(Download.this,"please input the right URl_address!", Toast.LENGTH_SHORT).show(); return; } //connect to the server and get the resource of picture new Thread(){ public void run(){ try{ //start the HTTP connection URL url_address=new URL(Url_address); HttpURLConnection conn=(HttpURLConnection)url_address.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); file_length=conn.getContentLength(); if (file_length <= 0) throw new RuntimeException(" can not get the inputStream "); if (inputStream == null) throw new RuntimeException("stream is null"); FileOutputStream output=new FileOutputStream("/sdcard/"+"image.jpg"); //storage the download file byte[] data=new byte[4096]; int count=0; sendMsg(0); //initialize the progressBar do { count= inputStream.read(data); if (count == -1) { break; } output.write(data, 0, count); downloadfilesize+= count; sendMsg(1); //update the progressBar } while (true); sendMsg(2); //view the picture }catch(MalformedURLException e1){ e1.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); }//end of onClick() }; }//end of the Download

layout中的代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <Button android:id="@+id/download" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start the download"> </Button> <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ProgressBar> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerInside" android:padding="2dp"> </ImageView> </FrameLayout> </LinearLayout>


http://www.niftyadmin.cn/n/1959124.html

相关文章

IOS开发:开发者账号申请后真机调试详细步骤

http://blog.csdn.net/luyuncsd123/article/details/17534515 前提&#xff1a;已经成功申请了iOS开发者账号 环境&#xff1a;xcode 4.6.1 &#xff1b; iOS 6.1.2 1、授权设备&#xff1a; 进入Apple Developer会员中心&#xff0c;点击图中的iOS Provisioning Portal&#x…

置换群-轮换、对换

置换群是由置换组成的群。设Ω为非空集合&#xff0c;设存在Ω上的一个置换 1、轮换 除()内的点参与轮换外&#xff0c;其余点不动 (a1,a2,a3,a4,a5....an)|a1 a2 a3 a4 a5 a6... an| |a2,a3,a4,a5,a6....an,a1| 比如n5,即1,2,3,4,5为集合内元素&#xff0c;(1,2,5)表示只有…

c-feof(fp)

feof(fp)有两个返回值:如果遇到文件结束&#xff0c;函数feof&#xff08;fp&#xff09;的值为1&#xff0c;否则为0。 EOF是文件结束标志的文件。在文本文件中&#xff0c;数据是以字符的ASCⅡ代码值的形式存放&#xff0c;ASCⅡ代码的范围是0到255&#xff0c;不可能出现-1…

U3D DrawCall优化手记

http://www.cnblogs.com/ybgame/p/3588795.html 在最近&#xff0c;使用U3D开发的游戏核心部分功能即将完成&#xff0c;中间由于各种历史原因&#xff0c;导致项目存在比较大的问题&#xff0c;这些问题在最后&#xff0c;恐怕只能通过一次彻底的重构来解决 现在的游戏跑起来会…

多项式-奇异性

1、行列式在数学中&#xff0c;是一个函数&#xff0c;其定义域为的矩阵A&#xff0c;取值为一个标量&#xff0c;写作det(A)或 | A | 。 2、 奇异矩阵是线性代数的概念&#xff0c;就是对应的行列式等于0的矩阵。 奇异矩阵的判断方法&#xff1a;首先&#xff0c;看这个矩阵是…

fps Ragdoll 的设置与用法

http://www.narkii.com/club/thread-297439-1.html 关于Ragdoll的制作方式FPS游戏中的敌人受到攻击后阵亡后&#xff0c;通常有两种动作&#xff0c;一种是设置敌人的Death动画进行播放&#xff0c;另一种则是将敌人换成ragdoll模式。以效果来看&#xff0c;ragdoll会得到比较真…

NLP实战6:seq2seq翻译实战-Pytorch复现-小白版

目录 一、前期准备 1. 搭建语言类 2. 文本处理函数 3. 文件读取函数 二、Seq2Seq 模型 1. 编码器&#xff08;Encoder&#xff09; 2. 解码器&#xff08;Decoder&#xff09; 三、训练 1. 数据预处理 2. 训练函数 四、训练与评估 &#x1f368; 本文为[&#x1f51…

并行计算介绍(Introduction to Parallel Computing ) ——我主张阅读英文原著

串行计算&#xff0c;分为“指令”和“数据”两个部分&#xff0c;在程序执行时“独立地申请和占有”内存空间&#xff0c;所有计算均局限于该内存空间。 并行计算&#xff0c;则将进程相对独立的分配在不同的节点上&#xff0c;由各自独立的操作系统调度&#xff0c;享有独立的…