码迷,mamicode.com
首页 > 移动开发 > 详细

安卓实训第五天

时间:2014-05-25 23:14:15      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:android   class   c   code   java   tar   

首先讲了如何将文件上传到SD卡中:

 

package com.example.lesson05_02;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.http.Header;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MainActivity extends Activity implements OnClickListener{

 private EditText et_file;
 private Button btn_upload;
 private EditText et_username;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn_upload = (Button) findViewById(R.id.btn_upload);
  
  et_file = (EditText) findViewById(R.id.et_file);
  et_username = (EditText) findViewById(R.id.edit_username);
  btn_upload.setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
  int id =v.getId();
  switch (id) {
  case R.id.btn_upload:
   String path =et_file.getText().toString();
   if(TextUtils.isEmpty(path.trim())){
    Toast.makeText(this, "上传的文件不能为空", Toast.LENGTH_LONG).show();
   }else{
    //异步客户端对象
    AsyncHttpClient client = new AsyncHttpClient();
    String url="http://172.16.237.144:8080/Login/UploadServlet";
    //封装文件上传的参数
    RequestParams params = new RequestParams();
    //根据路径创建文件
    File file = new File(path);
    try {
     //放入文件
     params.put("profile_picture", file);
     //1、修改这个名称,看能否上传成功
     //2、添加上传者的姓名
    } catch (FileNotFoundException e) {
     System.out.println("文件不存在");
     e.printStackTrace();
    }
    client.post(url, params,new AsyncHttpResponseHandler(){

     @Override
     public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
       Throwable error) {
      error.printStackTrace();
     }

     @Override
     public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
      Toast.makeText(MainActivity.this, "文件上传成功!", 1).show();
     }
      
    });
   }
   
   break;

  default:
   break;
  }
  
 }
}

然后分别使用AsynvHttpClient 还有smart-image-view来完成图片查看器:

//不适用SMART IMAGE之前:
 /*
 public void sendGetImg(View v){
  int id= v.getId();
  switch (id) {
  case R.id.btn_img:
   
   String url = et_url.getText().toString();
   //发送网络请求
   AsyncHttpClient client = new AsyncHttpClient();
   
   client.get(url,new AsyncHttpResponseHandler(){

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
      Throwable errors) {
      errors.printStackTrace();
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
     if(statusCode==200){
      //创建字节工厂对象N
      BitmapFactory bitmapFactory = new BitmapFactory();
      @SuppressWarnings("static-access")
      Bitmap bitmap = bitmapFactory.decodeByteArray(responseBody, 0, responseBody.length);
      iv_img.setImageBitmap(bitmap);
     }
    }
    
   });
   break;

  default:
   break;
  }
 }
 */
 //使用smart image包之后
 public void sendGetImg(View v){
  int id= v.getId();
  switch (id) {
  case R.id.btn_img:
   String url = et_url.getText().toString();
   iv_img.setImageUrl(url, R.drawable.ic_launcher,R.drawable.ic_launcher);
   break;

  default:
   break;
  }
 }

 

安卓实训第五天,布布扣,bubuko.com

安卓实训第五天

标签:android   class   c   code   java   tar   

原文地址:http://blog.csdn.net/lygscg123/article/details/26941591

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!