0

在我的 android 应用程序中,我能够捕获图像并存储在 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);
                }
4

1 回答 1

1

1)在您的服务器上创建一个网络服务

2)在android中将您的图像转换为Base64字符串

3) 通过 ksoap2 将该字符串发送到 Web 服务

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

或者

    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();
    }
于 2012-08-08T08:52:08.893 回答