求Android手机客户端上传图片到服务器方案。要求能断点续传,支持大图片、快速上传,稳定可靠,连接便捷

求Android手机客户端上传图片到服务器方案。要求能断点续传,支持大图片、快速上传,稳定可靠,连接便捷,第1张

断点要服务器支持的,普通的>//获取connection conn = getURLConnection(url); is = conngetInputStream(); //获取Bitmap的引用 Bitmap bitmap = BitmapFactorydecodeStream(is) //获取长度 int length = (int) conngetContentLength(); if (length != -1) { byte[] imgData = new byte[length]; byte[] temp=new byte[512]; int readLen=0; int destPos=0; while((readLen=isread(temp))>0){ Systemarraycopy(temp, 0, imgData, destPos, readLen); destPos+=readLen; } bitmap=BitmapFactorydecodeByteArray(imgData, 0, imgDatalength); }可能想得不是很完善,你可以参考参考。

首先是拍照:使用Intent即可,
[java] view plaincopyprint
01final String start = EnvironmentgetExternalStorageState();
02private static final String PHOTOPATH = "/photo/";
03
04if(startequals(EnvironmentMEDIA_MOUNTED)){
05Intent intent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);
06File file = new File(EnvironmentgetExternalStorageDirectory()+PHOTOPATH);
07if(!fileexists()){
08filemkdirs();
09}
10tempphontname = SystemcurrentTimeMillis()+"jpg";
11bufferappend(EnvironmentgetExternalStorageDirectory()+PHOTOPATH)append(tempphontname);
12intentputExtra(MediaStoreEXTRA_OUTPUT, UrifromFile(new File(buffertoString())));
13startActivityForResult(intent, 1);
14}
final String start = EnvironmentgetExternalStorageState();
private static final String PHOTOPATH = "/photo/";
if(startequals(EnvironmentMEDIA_MOUNTED)){
Intent intent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);
File file = new File(EnvironmentgetExternalStorageDirectory()+PHOTOPATH);
if(!fileexists()){
filemkdirs();
}
tempphontname = SystemcurrentTimeMillis()+"jpg";
bufferappend(EnvironmentgetExternalStorageDirectory()+PHOTOPATH)append(tempphontname);
intentputExtra(MediaStoreEXTRA_OUTPUT, UrifromFile(new File(buffertoString())));
startActivityForResult(intent, 1);
}


其次是从本地相册选:依旧是Intent
如下代码:
[java] view plaincopyprint
01if(startequals(EnvironmentMEDIA_MOUNTED)){
02 Intent getImage = new Intent(IntentACTION_GET_CONTENT);
03 getImageaddCategory(IntentCATEGORY_OPENABLE);
04 getImagesetType("image/jpeg");
05 startActivityForResult(getImage, 0);
06}
if(startequals(EnvironmentMEDIA_MOUNTED)){
Intent getImage = new Intent(IntentACTION_GET_CONTENT);
getImageaddCategory(IntentCATEGORY_OPENABLE);
getImagesetType("image/jpeg");
startActivityForResult(getImage, 0);
}
接下来是主要的:因为调用完系统的方法后,回返回到回调方法onActivityResult(int, int, Intent)中,
在里面进行主要的照片上传服务器的 *** 作,
见代码:
[java] view plaincopyprint
01@Override
02 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
03 ContentResolver resolver = getContentResolver();
04 if(requestCode==1)//
05 {
06 if(resultCode==ActivityRESULT_OK)
07 {
08 if(EnvironmentgetExternalStorageState()equals(EnvironmentMEDIA_MOUNTED))
09 {
10ew Thread(new Runnable()
11 {
12 @Override
13 public void run() {
14//进行上传 *** 作
15}
16}start();
转载


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10939433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存