x 5 cloudクラウドプラットフォームを使用してネットカラーメッセージを作成----------ファイルアップロードダウンロード進捗バー使用(8)


プログレスバーは頭が痛い問題で、もちろん分かってもよく理解できて、使うのも便利で、2つに及ぶことが多いです.1つは円形で、もう1つは長方形で、少しずつ進んでいます.
円形については簡単ですが、長方形の原理についてお話しします.
       まずファイルサイズを取得し、ループ書きの時に加算減算演算を行う必要があります.ははは、こんなに簡単です.
例をあげましょう.
public class Android_X5_SOSO_9_9_2Activity extends Activity {
    /** Called when the activity is first created. */
	ProgressBar pb;
	TextView tv;
	int   fileSize;
	int   downLoadFileSize;
	String fileEx,fileNa,filename;
	private Handler handler = new Handler()
	  {
	    @Override
	    public void handleMessage(Message msg)
	    {//    Handler,         UI   
	      if (!Thread.currentThread().isInterrupted())
	      {
	        switch (msg.what)
	        {
	          case 0:
	            pb.setMax(fileSize);
	          case 1:
	            pb.setProgress(downLoadFileSize);
	            int result = downLoadFileSize * 100 / fileSize;
	            tv.setText(result + "%");
	            break;
	          case 2:
	            Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, "      ", 1000).show();
	            break;
 
	          case -1:
	            String error = msg.getData().getString("error");
	            Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, error, 1000).show();
	            break;
	        }
	      }
	      super.handleMessage(msg);
	    }
	  };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        pb=(ProgressBar)findViewById(R.id.down_pb);
        tv=(TextView)findViewById(R.id.tv);
        new Thread(){
        	public void run(){
        		try {
        			String localpath=Environment
        			.getExternalStorageDirectory()
        			.getAbsolutePath()+"/CXSOSO/"
        			+"20110901005.3gp"; 
					down_file(localpath,"/sdcard/");
					//    ,  :   URL,       
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        }.start();
 
 
    }
    public void down_file(String url,String path) throws IOException{
    	//       	
    	filename=url.substring(url.lastIndexOf("/") + 1);
    	//     
    	/*URL myURL = new URL(url);
    	URLConnection conn = myURL.openConnection();
    	conn.connect();*/
    	/*InputStream is = conn.getInputStream();
	    this.fileSize = conn.getContentLength();//          
*/	    
    	File file = new File(url);  
    	DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); 
    	try {
			this.fileSize=read(file).length;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	if (this.fileSize <= 0) throw new RuntimeException("         ");
	    if (is == null) throw new RuntimeException("stream is null");
	    FileOutputStream fos = new FileOutputStream(path+filename);
	    //       +   
	    byte buf[] = new byte[1024];
	    downLoadFileSize = 0;
	    sendMsg(0);
	    do
	      {
	    	//    
	        int numread = is.read(buf);
	        if (numread == -1)
	        {
	          break;
	        }
	        fos.write(buf, 0, numread);
	        downLoadFileSize += numread;
 
	        sendMsg(1);//     
	      } while (true);
	    sendMsg(2);//      
	    try
	      {
	        is.close();
	      } catch (Exception ex)
	      {
	        Log.e("tag", "error: " + ex.getMessage(), ex);
	      }
 
    }
	private void sendMsg(int flag)
	{
	    Message msg = new Message();
	    msg.what = flag;
	    handler.sendMessage(msg);
	}	 
	 public byte[] read(File myfile) throws Throwable{ 
			//     ,      fpath 
			 DataInputStream inStream = new DataInputStream(new BufferedInputStream(new FileInputStream(myfile))); 
		     byte[] data = readFile(inStream); 
//		     data.length;
		     return data; 
		} 
	    public static byte[] readFile(InputStream inStream) throws Throwable{ 
		     int len = 0; 
		     byte[] buffer = new byte[1024]; 
		     ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
		     while((len = inStream.read(buffer))!=-1){ 
		         outStream.write(buffer,0,len); 
		     } 
		     outStream.close(); 
		     return outStream.toByteArray(); 
		} 
 
}