将选定的图像从SD卡上传到php服务器

问题描述:

在我的android应用程序中,我能够捕捉图像并存储在SD卡中。我有一个选择按钮和复选框来选择图片。但我不知道如何上传选定的图像到一个PHP服务器,通过我的网站显示。代码发布如下,请告诉如何上传这些选定的图像。谢谢将选定的图像从SD卡上传到php服务器

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); 
       } 
+0

你打算如何将它们存储在服务器上?你的服务器代码是什么样的? – 2012-08-08 08:40:34

+1

可以放上UploadQueue.class文件代码你可以使用字节数组上传或者你可以上传整个图像到服务器。如果您使用的是字节数组,然后服务器需要解码该图像 – Sumant 2012-08-08 08:42:11

+0

@GlennBech听说Base64,所以我在找那 – 2012-08-08 09:02:40

1)服务器

2)图像转换为机器人

3 Base64编码字符串)发送字符串到web服务对ksoap2

4创建一个Web服务)将字符串转换回web服务中的图像(如果不需要,则不需要将其转换为图像文件)

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; 
} 

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(); 
    } 
+0

请问如何将图像转换为Base64?你能分享一些代码吗?这将会非常有帮助 – 2012-08-08 09:06:41