我写了一个多图片上传的小程序,选出的图片存在List集合里,然后通过for循环发送到服务器里。可是却发生了收到都是最后一张图片,这该怎么解决!求大神相助!!!!!!!!!!!!!!!!!!!!
应该是服务器那边的问题,服务器在每个上传的图片都应该保存到不同的文件名上
服务器那边没处理好。。。
使用imageloader
应该是你服务器没有为每次循环new的对象,用的同一个对象,检查你的循环
把代码贴出来,这样才能指出你要修改哪里
访问网络是一个耗时的操作,程序会优先处理UI主线程,执行完for语句之后再跳转到子线程去请求服务器,于是乎。。。每次都只能上传一张,而且是最后那张。这是我个人的理解。最近也在想多图片上传的问题,学习学习!!
public class MainActivity extends Activity implements OnClickListener {
private static String requestURL = "http://192.168.191.1:8080/Upload/UploadServlet";
private Button selectImage, uploadImage;
private ImageView imageView, imageView1;
private String picPath = null;
private static final String TAG = "uploadFile";
private File file;
private List<Store> liststore = new ArrayList<Store>();
private Store store;
private ListView listview;
private List<Picture> list = new ArrayList<Picture>();
private PictureAdapter adapter;
private Handler handle = new Handler() {
public void handleMessage(Message msg) {
String request = (String) msg.obj;
uploadImage.setText(request);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectImage = (Button) this.findViewById(R.id.selectImage);
uploadImage = (Button) this.findViewById(R.id.uploadImage);
listview = (ListView) this.findViewById(R.id.listview);
selectImage.setOnClickListener(this);
uploadImage.setOnClickListener(this);
// imageView = (ImageView) this.findViewById(R.id.picture_image);
// imageView1 = (ImageView) this.findViewById(R.id.imageView1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selectImage:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
break;
case R.id.uploadImage:
// if (picPath == null) {
// Toast.makeText(MainActivity.this, "请选择图片!", Toast.LENGTH_SHORT)
// .show();
// }else {
String getstorepath = null;
for (int i = 0; i < liststore.size(); i++) {
// for(Store o:liststore){
getstorepath = store.getpaths();
file = new File(getstorepath);
if (file != null) {
new Thread() {
public void run() {
String request = String.valueOf(UploadUtil
.uploadFile(file, requestURL));
Message msg = handle.obtainMessage();
msg.obj = request;
handle.sendMessage(msg);
}
}.start();
String request = "uploading...";
uploadImage.setText(request);
}
// }
}
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Log.e(TAG, "uri = " + uri);
try {
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
Log.e(TAG, path);
if (path.endsWith("jpg") || path.endsWith("png")) {
picPath = path;
Bitmap bitmap = BitmapFactory.decodeStream(cr
.openInputStream(uri));
// imageView.setImageBitmap(bitmap);
//这部分是预览功能,能正常运行
Picture picture = new Picture(bitmap);
list.add(picture);
adapter = new PictureAdapter(this, R.layout.item_menu,
list);
listview.setAdapter(adapter);
//这里是存储点击调用的图片,可是我测试了总显示存储同一张图片
store = new Store(picPath);
liststore.add(store);
} else {
alert();
}
} else {
alert();
}
} catch (Exception e) {
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//这是我用来存储图片路径的类
public class Store {
private String paths;
private static final String TAG = "uploadFile";
public Store(String paths) {
this.paths = paths;
}
public String getpaths() {
Log.e(TAG, paths);
return paths;
}
}
试试这个ExecutorService