利用AsyncHttpClient实现图片的上传与下载
2014-12-19 15:04
134 查看
图片上传
[java] view
plaincopy
/**
* @param path
* 要上传的文件路径
* @param url
* 服务端接收URL
* @throws Exception
*/
public static void uploadFile(String path, String url) throws Exception {
File file = new File(path);
if (file.exists() && file.length() > 0) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("uploadfile", file);
// 上传文件
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// 上传成功后要做的工作
Toast.makeText(mContext, "上传成功", Toast.LENGTH_LONG).show();
progress.setProgress(0);
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// 上传失败后要做到工作
Toast.makeText(mContext, "上传失败", Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 上传进度显示
progress.setProgress(count);
Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
}
@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重试次数
}
});
} else {
Toast.makeText(mContext, "文件不存在", Toast.LENGTH_LONG).show();
}
图片下载
[java] view
plaincopy
/**
* @param url
* 要下载的文件URL
* @throws Exception
*/
public static void downloadFile(String url) throws Exception {
AsyncHttpClient client = new AsyncHttpClient();
// 指定文件类型
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
// 获取二进制数据如图片和其他文件
client.get(url, new BinaryHttpResponseHandler(allowedContentTypes) {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] binaryData) {
String tempPath = Environment.getExternalStorageDirectory()
.getPath() + "/temp.jpg";
// TODO Auto-generated method stub
// 下载成功后需要做的工作
progress.setProgress(0);
//
Log.e("binaryData:", "共下载了:" + binaryData.length);
//
Bitmap bmp = BitmapFactory.decodeByteArray(binaryData, 0,
binaryData.length);
File file = new File(tempPath);
// 压缩格式
CompressFormat format = Bitmap.CompressFormat.JPEG;
// 压缩比例
int quality = 100;
try {
// 若存在则删除
if (file.exists())
file.delete();
// 创建文件
file.createNewFile();
//
OutputStream stream = new FileOutputStream(file);
// 压缩输出
bmp.compress(format, quality, stream);
// 关闭
stream.close();
//
Toast.makeText(mContext, "下载成功\n" + tempPath,
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] binaryData, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "下载失败", Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 下载进度显示
progress.setProgress(count);
Log.e("下载 Progress>>>>>", bytesWritten + " / " + totalSize);
}
@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重试次数
}
});
PHP服务端
[php] view
plaincopy
<?php
$base_path = "./upload/"; // 接收文件目录
$target_path = $base_path . basename ( $_FILES ['uploadfile'] ['name'] );
if (move_uploaded_file ( $_FILES ['uploadfile'] ['tmp_name'], $target_path )) {
$array = array (
"code" => "1",
"message" => $_FILES ['uploadfile'] ['name']
);
echo json_encode ( $array );
} else {
$array = array (
"code" => "0",
"message" => "There was an error uploading the file, please try again!" . $_FILES ['uploadfile'] ['error']
);
echo json_encode ( $array );
}
?>
Service中类似的异常问题:
(com.zajt.zajtsafechat.http.AsyncHttpResponseHandler$ResponderHandler) {42843450} sending message to a Handler on a dead thread
解决办法:
在Service中,AsyncHttpClient client = new SyncHttpClient();
在Activity中,AsyncHttpClient client = new AsyncHttpClient();
其中SyncHttpClient是AsyncHttpClient的子类
[java] view
plaincopy
/**
* @param path
* 要上传的文件路径
* @param url
* 服务端接收URL
* @throws Exception
*/
public static void uploadFile(String path, String url) throws Exception {
File file = new File(path);
if (file.exists() && file.length() > 0) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("uploadfile", file);
// 上传文件
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// 上传成功后要做的工作
Toast.makeText(mContext, "上传成功", Toast.LENGTH_LONG).show();
progress.setProgress(0);
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// 上传失败后要做到工作
Toast.makeText(mContext, "上传失败", Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 上传进度显示
progress.setProgress(count);
Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
}
@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重试次数
}
});
} else {
Toast.makeText(mContext, "文件不存在", Toast.LENGTH_LONG).show();
}
图片下载
[java] view
plaincopy
/**
* @param url
* 要下载的文件URL
* @throws Exception
*/
public static void downloadFile(String url) throws Exception {
AsyncHttpClient client = new AsyncHttpClient();
// 指定文件类型
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
// 获取二进制数据如图片和其他文件
client.get(url, new BinaryHttpResponseHandler(allowedContentTypes) {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] binaryData) {
String tempPath = Environment.getExternalStorageDirectory()
.getPath() + "/temp.jpg";
// TODO Auto-generated method stub
// 下载成功后需要做的工作
progress.setProgress(0);
//
Log.e("binaryData:", "共下载了:" + binaryData.length);
//
Bitmap bmp = BitmapFactory.decodeByteArray(binaryData, 0,
binaryData.length);
File file = new File(tempPath);
// 压缩格式
CompressFormat format = Bitmap.CompressFormat.JPEG;
// 压缩比例
int quality = 100;
try {
// 若存在则删除
if (file.exists())
file.delete();
// 创建文件
file.createNewFile();
//
OutputStream stream = new FileOutputStream(file);
// 压缩输出
bmp.compress(format, quality, stream);
// 关闭
stream.close();
//
Toast.makeText(mContext, "下载成功\n" + tempPath,
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] binaryData, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "下载失败", Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// TODO Auto-generated method stub
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 下载进度显示
progress.setProgress(count);
Log.e("下载 Progress>>>>>", bytesWritten + " / " + totalSize);
}
@Override
public void onRetry(int retryNo) {
// TODO Auto-generated method stub
super.onRetry(retryNo);
// 返回重试次数
}
});
PHP服务端
[php] view
plaincopy
<?php
$base_path = "./upload/"; // 接收文件目录
$target_path = $base_path . basename ( $_FILES ['uploadfile'] ['name'] );
if (move_uploaded_file ( $_FILES ['uploadfile'] ['tmp_name'], $target_path )) {
$array = array (
"code" => "1",
"message" => $_FILES ['uploadfile'] ['name']
);
echo json_encode ( $array );
} else {
$array = array (
"code" => "0",
"message" => "There was an error uploading the file, please try again!" . $_FILES ['uploadfile'] ['error']
);
echo json_encode ( $array );
}
?>
Service中类似的异常问题:
(com.zajt.zajtsafechat.http.AsyncHttpResponseHandler$ResponderHandler) {42843450} sending message to a Handler on a dead thread
解决办法:
在Service中,AsyncHttpClient client = new SyncHttpClient();
在Activity中,AsyncHttpClient client = new AsyncHttpClient();
其中SyncHttpClient是AsyncHttpClient的子类
相关文章推荐
- 利用AsyncHttpClient实现图片的上传与下载
- 利用AsyncHttpClient实现图片的上传与下载+进度条
- AsyncHttpClient实现上传 HttpURLConnection实现下载
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- 利用javascript剪裁图片(也许CSDN也是如此实现图像上传剪裁)
- 利用WebService实现远程服务器文件的上传和下载
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- Android利用webservices实现图片远程上传
- 利用javascript从数据库取数据来实现CSDN首页图片的切换效果(新增下载地址)
- ASP.NET2.0中全面实现文件图片上传下载处理
- 浅谈利用jspsmart实现文件上传下载(转)
- 利用zend framework的Zend_File_Transfer_Adapter_Http类实现多个图片的上传
- android之调用webservice 实现图片上传下载
- 利用.NET类库实现图片下载和Ping命令
- 利用WebService实现远程服务器文件的上传和下载
- 利用FTPClient类实现文件的上传下载功能
- struts实现的图片的上传和下载
- 利用fleximage实现图片上传
- 利用WebService实现远程服务器文件的上传和下载
- android之调用webservice 实现图片上传下载