Android 将所选图像从SD卡上载到php服务器
在我的android应用程序中,我能够捕获图像并存储在SD卡中。我有一个选择按钮和复选框来选择图片。但我不知道如何将选定的图像上传到php服务器,以便通过我的网站显示。下面是代码,请告诉我们如何上传这些选定的图片。多谢各位Android 将所选图像从SD卡上载到php服务器,android,camera,sd-card,image-uploading,Android,Camera,Sd Card,Image Uploading,在我的android应用程序中,我能够捕获图像并存储在SD卡中。我有一个选择按钮和复选框来选择图片。但我不知道如何将选定的图像上传到php服务器,以便通过我的网站显示。下面是代码,请告诉我们如何上传这些选定的图片。多谢各位 imageAdapter = new ImageAdapter(); imageAdapter.initialize(); imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); imagegrid.setAdapt
imageAdapter = new ImageAdapter();
imageAdapter.initialize();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(imageAdapter);
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final int len = imageAdapter.images.size();
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (imageAdapter.images.get(i).selection) {
cnt++;
selectImages = selectImages
+ imageAdapter.images.get(i).id + ",";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG).show();
} else {
selectImages = selectImages.substring(0,selectImages.lastIndexOf(","));
Intent intent = new Intent(MainActivity.this,
UploadQueue.class);
intent.putExtra("Ids", selectImages);
startActivityForResult(intent, UPLOAD_IMAGES);
}
imageAdapter=新的imageAdapter();
imageAdapter.initialize();
imagegrid=(GridView)findViewById(R.id.PhoneImageGrid);
setAdapter(imageAdapter);
最终按钮selectBtn=(按钮)findViewById(R.id.selectBtn);
选择btn.setOnClickListener(新建OnClickListener(){
公共void onClick(视图v){
final int len=imageAdapter.images.size();
int-cnt=0;
字符串selectImages=“”;
对于(int i=0;i
1)在服务器上创建Web服务
2) 在android中将图像转换为Base64字符串
3) 通过ksoap2将该字符串发送到webservice
4) 在webservice中将字符串转换回图像(如果不需要,则无需将其转换为图像文件)
5) 将其保存在服务器的硬盘上
编辑:
public static Bitmap base64ToBitmap(String strBase64) throws IOException {
byte[] bitmapdata = Base64.decode(strBase64);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0,
bitmapdata.length);
return bitmap;
}
public static String bitmapToBase64(Bitmap bitmap) {
byte[] bitmapdata = bitmapToByteArray(bitmap);
return Base64.encodeBytes(bitmapdata);
}
public static byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
或
您计划如何将它们存储在您的服务器上?您的服务器代码是什么样子的?您可以将UploadQueue.class文件代码放在服务器上吗?您可以使用字节数组上载,也可以将整个图像上载到服务器。如果您使用字节数组,则服务器需要对其进行解码image@GlennBech听说过Base64,所以我在找它。你能告诉我如何使用c吗将图像转换为Base64?您可以共享一些代码吗?这将非常有用
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
public static String fileToBase64(String path) throws IOException {
byte[] bytes = fileToByteArray(path);
Base64.encodeBytes(bytes);
}
public static void base64ToFile(String path, String strBase64)
throws IOException {
byte[] bytes = Base64.decode(strBase64);
byteArrayTofile(path, bytes);
}
public static void byteArrayTofile(String path, byte[] bytes)
throws IOException {
File imagefile = new File(path);
File dir = new File(imagefile.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(imagefile);
fos.write(bytes);
fos.close();
}