将图像从Android上传到服务器 - 仅上传图像的一部分

时间:2014-12-04 03:36:02

标签: php android image

我的应用程序需要将多个图像上传到服务器。我通过解码Base64发送图像。

        Bitmap bitmapOrg = BitmapFactory.decodeFile(imagePath);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte [] ba = bao.toByteArray();
        ba1 = Base64.encodeBytes(ba);

PHP

       $path = "/".$filename.".jpeg";
       $buffer = base64_decode($base);
       $handle = fopen($path, 'wb');
       $numbytes = fwrite($handle, $buffer);
       fclose($handle);
       ob_clean();
       flush();

现在我可以看到,有些图片只是部分上传。我只能看到图像的最上面几位,而且在服务器中都是空白的。可能有什么问题?使用Base64解码有什么缺点吗?

2 个答案:

答案 0 :(得分:0)

为文件尝试checksum以确保文件完全上传

您可以检查此link以生成android

中文件的校验和

答案 1 :(得分:-1)

请使用此方法。我希望它对您有用。在您的代码中,可能是图像像素显示得那么高。然后您无法在服务器中看到图像。

/**
     * encodes image to string using Base64
     * 
     * @return String imageString
     */
    **

private String prepareImage() {
        if (tempPath == null || (!isProfilechanged)) {
            return "";
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(tempPath, options);
        bitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        byte[] byteArray = baos.toByteArray();
        String imageString = com.qwiches.utils.Base64.encodeBytes(byteArray);
        bitmap = null;
        System.gc();
        Runtime.getRuntime().gc();
        return imageString;
    }

**