标签: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;
}
}
标签:android class c code java tar
原文地址:http://blog.csdn.net/lygscg123/article/details/26941591