Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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中,如何使用函数“getAttribute”从img和exif中提取字节数组?_Java_Android_Exif - Fatal编程技术网

Java 在Android中,如何使用函数“getAttribute”从img和exif中提取字节数组?

Java 在Android中,如何使用函数“getAttribute”从img和exif中提取字节数组?,java,android,exif,Java,Android,Exif,我是JAVA编程的新手 我想使用android.media.ExifInterface来保存和恢复一些字节数组作为exif信息 String str = new String(byteArray);//save exif.setAttribute(ExifInterface.TAG_MAKER_NOTE, str); exif.saveAttributes(); String str =exif.getAttribute(ExifInterface.TAG_MAKER_NOTE

我是JAVA编程的新手

我想使用android.media.ExifInterface来保存和恢复一些字节数组作为exif信息

  String str = new String(byteArray);//save
  exif.setAttribute(ExifInterface.TAG_MAKER_NOTE, str);
  exif.saveAttributes();

  String str =exif.getAttribute(ExifInterface.TAG_MAKER_NOTE);//restore
  if(str != null)
  {
    byte[] byteArray = str.getBytes(); 
  }
首先,我使用Stringbyte[]将byte[]转换为String。 然后我使用函数setAttributeString tag,String value将字符串与标记tag\u MAKER\u NOTE一起保存。 当我想提取byteArray时,我会使用getAttributeString标记来获取相应的字符串

但是如果保存的字节数组如下所示,我发现函数getAttributeString标记无法正常工作:

 byte[] byteArray = new byte[]{ 1,2,3,4,0,0,5,6};
返回的字符串仅包含{1,2,3,4}。0之后会丢失数据。字符串长度为4,而保存的字符串为正常字符串。也许字符串将0视为结尾

我想知道有没有办法提取整个字节数组?没有第三个库更好。

使用base64编码的字符串,而不是使用新的StringbyteArray转换为字符串。代码如下所示:

byte[] byteArray = new byte[]{1, 2, 3, 4, 0, 0, 5, 6};
String str = Base64.getEncoder().encodeToString(byteArray);
System.out.println(str);
byte[] result = Base64.getDecoder().decode(str);
System.out.println(Arrays.toString(result));

这个问题似乎和特德列夫所说的一样。它与字符串转换有关,而不是保存和存储。Java使用Unicode,字符0是空字符,通常用于字符串的结尾。您正在尝试存储和检索一个数字列表吗?是的,我想存储和检索一个二进制数据列表,就像cpp中的memcpy一样。但是我保存之前的字符串与我从Exify中还原的不同。你必须找到一种方法,将二进制数据编码为可以完整解码的字符串。接受byte[]参数的字符串构造函数不适用于您,因为有些值组合会生成无效文本。像tdelev建议的Base64是一种方法。