Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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 如何在android中将图像转换为十六进制字符串?_Java_Android_Hex_Type Conversion_Image Conversion - Fatal编程技术网

Java 如何在android中将图像转换为十六进制字符串?

Java 如何在android中将图像转换为十六进制字符串?,java,android,hex,type-conversion,image-conversion,Java,Android,Hex,Type Conversion,Image Conversion,我需要转换成十六进制字符串图像发送到网络服务器。我使用这个方法将图像转换为字节数组 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options); int size = rece

我需要转换成十六进制字符串图像发送到网络服务器。我使用这个方法将图像转换为字节数组

         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8; 
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);
         int size = receipt.getRowBytes() * receipt.getHeight();  
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         receipt.compress(Bitmap.CompressFormat.JPEG, 90, stream);
         receiptbyte = stream.toByteArray();   
         String hexstring = toHex(receiptbyte);  
这个要转换成十六进制

   public static String toHex(byte[] bytes) {
    BigInteger bi = new BigInteger(1, bytes); 
    return String.format("%0" + (bytes.length << 1) + "X", bi);
}
我想像这样产生输出

c11ee236-8f72-4b60-9208-79977d61993f

我不知道该怎么办。我需要对它进行编码吗

像c11ee236-8f72-4b60-9208-79977d61993f这样的字符串不是映像-它看起来更像是存储在服务器上的映像的ID

如果需要图像,则必须将ID发送到服务器,服务器会发回存储在其数据库中的属于该ID的图像数据

在Java中,您可以自己生成这样的随机ID:

UUID u = UUID.randomUUID();
System.out.println(u.toString());
输出例如:3aa5b32d-c6fb-43c5-80c9-78a1a35aff40

构建您自己的服务器您可以使用它并将图像数据和此ID保存到数据库中。

您拥有的字符串(如c11ee236-8f72-4b60-9208-79977d61993f)不是图像-它看起来更像存储在服务器上的图像的ID

如果需要图像,则必须将ID发送到服务器,服务器会发回存储在其数据库中的属于该ID的图像数据

在Java中,您可以自己生成这样的随机ID:

UUID u = UUID.randomUUID();
System.out.println(u.toString());
输出例如:3aa5b32d-c6fb-43c5-80c9-78a1a35aff40

构建您自己的服务器您可以使用它,并将图像数据和此ID保存到数据库中。

您可以这样做

//encode image to base64 string
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
 
        //decode base64 string to image
        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        image.setImageBitmap(decodedImage);
你可以这样做

//encode image to base64 string
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
 
        //decode base64 string to image
        imageBytes = Base64.decode(imageString, Base64.DEFAULT);
        Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        image.setImageBitmap(decodedImage);

看看这个帖子:看看这个帖子:谢谢你的回答。。。太棒了,不是图像,而是图像的id。。。您知道如何生成该id并将其链接到服务器吗。。实际上这就是我要找的。。这个答案很好,但是在看到这个答案之前,我发现。。但你的答案指向正确的方向。。谢谢你的回答,谢谢你的回答。。。太棒了,不是图像,而是图像的id。。。您知道如何生成该id并将其链接到服务器吗。。实际上这就是我要找的。。这个答案很好,但是在看到这个答案之前,我发现。。但你的答案指向正确的方向。。谢谢,我正在批改答案