Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换为基数64,结果为reapeat?_Java_Android - Fatal编程技术网

Java 转换为基数64,结果为reapeat?

Java 转换为基数64,结果为reapeat?,java,android,Java,Android,我在android中获取url,并使用以下代码将数据流转换为64位数据字符串: URL url = new URL("http://iranassistance.com/images/sos-logo.png"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); //urlConnection.setDoOutput

我在android中获取url,并使用以下代码将数据流转换为64位数据字符串:

URL url = new URL("http://iranassistance.com/images/sos-logo.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
//urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
String filename="downloadedFile.png";
Log.i("Local filename:",""+filename);
File file = new File(SDCardRoot,filename);
if(file.createNewFile()) {
  file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();byte[] imageBytes = new byte[urlConnection.getContentLength()];
inputStream.read(imageBytes, 0, imageBytes.length);
inputStream.close();
String base64Image = Base64.encodeToString(imageBytes, Base64.DEFAULT);
但是base64Image结果不完整,给出了如下结果:

…nUTJaJnb7PLyscfBMQLLiexyKSEh/O2RFCTCZTC8HR5xCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


重复的“A”表示有问题,图像不完整!为什么不能正常工作?

您可以使用以下函数将图像转换为base64,只需传递图像即可

private String encodeImage(Bitmap mphoto)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mphoto.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String encImage = Base64.encodeToString(b, Base64.DEFAULT);

    return encImage;
}
简单:

inputStream.read(imageBytes, 0, imageBytes.length);
您假设上面的代码总是一次读取所有字节

错。此方法读取它想要读取的字节数。因此,它将返回读取的字节数。见其:

返回:读取到缓冲区的总字节数,如果由于到达流的结尾而没有更多数据,则返回-1

换句话说:你必须循环和累积这些数字,直到你得到你想要的字节数


你得到了这些字符:你的数组最初都是0。如前所述:您只填充了该数组的一部分。所以剩下的数组中仍然有0,编码后的结果是aaaas。

GhostCat给出了正确的答案, 我将我的代码更改为以下代码,它可以工作:

 InputStream is = null;
    try {

        URL url = new URL("http://iranassistance.com/images/sos-logo.png");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        is = url.openStream ();
        byte[] byteChunk = new byte[4096]; 
        int n;

        while ( (n = is.read(byteChunk)) > 0 ) {
            baos.write(byteChunk, 0, n);
        }

        String base64Image2 = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
        db.UpdateImage64(base64Image2);
        productModel pd = db.GetProductById(2);

    }
    catch (IOException e) {
        e.printStackTrace ();

    }
    finally {
        if (is != null) {
            try{
                is.close();
            }
                catch (IOException s){

                }
        }
    }

我必须将InputStream更改为Base64才能保存到数据库中。它不需要位图!这不是问题所在。阅读部分是错误的。当要编码的字节不完整时,查看编码是没有帮助的。当您有图像url时,请首先将其转换为位图,然后可以调用函数e.g。URL=新URL;HttpURLConnection connection1=HttpURLConnection url.openConnection;连接1.setDoInputtrue;连接1.连接;InputStream input1=connection1.getInputStream;位图myBitmap1=BitmapFactory.decodeStreaminput1;ByteArrayOutputStream byteArrayOutputStream1=新建ByteArrayOutputStream;myBitmap1.compressBitmap.CompressFormat.PNG,100,byteArrayOutputStream1;字节[]byteArray1=byteArrayOutputStream1.toByteArray;encoded1=Base64.encodeToString字节数组1,Base64.DEFAULT;