无法将Base64字符串放在json中

时间:2016-07-10 13:02:07

标签: android json base64

我的Base64字符串是从照片转换而来的。我尝试将它们放在我的json对象中,但它会导致部分Base64字符串被剪切,甚至其他json对象也会消失。当我用短的Base64字符串替换原来的那个时,json格式就可以了。我怎么能修好它?

  public void TakePhotoOnClick(View view) {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    file = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    file = new File(file, "picture.jpg");
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    if (isIntentAvailable(this, cameraIntent)) {
        startActivityForResult(cameraIntent, REQUEST_TAKE_PICTURE);
    } else {
        Toast.makeText(this, R.string.msg_NoCameraAppsFound,
                Toast.LENGTH_SHORT).show();
    }

}

private boolean isIntentAvailable(Context context, Intent intent) {
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.w(" MainActivity","onActivityResult");
    if (requestCode == REQUEST_TAKE_PICTURE && resultCode == RESULT_OK) {

        Bitmap picture = BitmapFactory.decodeFile(file.getPath());
        ivDriverPhoto.setImageBitmap(picture);

        ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        picture.compress(Bitmap.CompressFormat.JPEG, 50, out1);
        image = out1.toByteArray();
        driver_photo = Base64.encodeToString(image, Base64.DEFAULT);
        Log.w(" MainActivity","image"+ driver_photo );

    }

}

照片Base64字符串类似于以下字符串: 图像/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9 ... (它完全显示它太长了。)

短测试Base64 Sting如下: 图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1 + jfqAAAAAnNCSVQICFXsRgQAAAAJcEhZcwAAAHoAAAB6AZUMqX8AAAAZdEVYdFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAA4ElEQVQoz6XRzyvDcRgH8JdJObmsJMUf4KIotaJEUYrLNPnxTY4r3Ocw32K + HwpxmZ1cNH / C / rzHYVvNZQ56X1 / P09P7EcbHn0AmH5OMYux8QQqmXSk8K8yasCWX2zMZpD44txSCDccOVZVsaqiPgm + v2roqntxpq / n0oTQEy05CcGnem1YIrnSUByDIzVlwY1FNS + bIix1rww3beh4kPR119y6cSs40JJIDTeuaCquSjtsQXHu32wePSkHZTNA0paFqX2FCkij6VwyKyUOwohKCpCDTHan261fRXZl / F / MHNSqo8Xq9umsAAAAASUVORK5CYII =

1 个答案:

答案 0 :(得分:0)

我通过使用BitmapFactory来减少图片大小来解决问题,而Base64字符串足够短以放入json。