在我的应用程序中,我正在用相机拍照。该文件存储在SD卡上。活动的onResult()方法将文件提供给另一个创建MyPic对象的类Event:
class Event {
public processResult(File f) {
MyPic myPic = new MyPic(f);
ExifHelper.setExif(f);
sendImage(myPic);
}
private void sendImage(MyPic myPic) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("myPic", myPic.getBase64());
URL url = new URL("http://anUrl.de/test.php");
byte[] data = jsonObject.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(data.length);
conn.connect();
OutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(data);
os.flush();
[...]
} catch(JSONException e) {
// Log.e(...)
}
}
}
class MyPic {
public final File pic;
public MyPic(File pic) {
this.pic = pic;
}
public String getBase64() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bm = BitmapFactory.decodeFile(pic.getAbsolutePath());
// Try 1:
// bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// bm.resize();
// return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
// Try 2:
ByteBuffer buffer = ByteBuffer.allocate(bm.getByteCount();
bm.copyPixelsToBuffer(buffer);
return buffer.array();
}
}
public class ExifHelper {
// Just an example
public static void setAttribute(File f) throws IOException {
ExifInterface exifInterface = new ExifInterface(f.getAbsolutePath());
exifInterface.setAttribute("Artist", "Foo");
exifInterface.setAttribute("XPAuthor", "Foo");
exifInterface.saveAttributes();
}
}
创建MyPic后,调用静态类中的setExif(File f)ExifHelper。 最后,使用sendImage()发送图像。
这是test.php:
<?php
$json = json_decode(file_get_contents('php://input'), true);
print file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/test/image.jpg', base64_decode($json['pic']));
?>
SD卡上的图像包含exif数据&#34;艺术家&#34;:&#34; Foo&#34;。我可以通过Android设备监视器下载它,Windows 8.1显示它。
如果我尝试通过ExifInterface.getAttribute(&#34; Artist&#34;)或ExifInterface.getAttribute(&#34; XPAuthor&#34;)读取标记,则返回null。
服务器上创建的图像(debian)不包含标记。
我已经尝试了很多东西,但却无法让它跑起来。我还考虑过使用PEL php库设置exif数据,但手机上也需要数据,两次做这项工作似乎很愚蠢。也许我写的小PHP脚本清除了exifdata?
有人可以帮我解决这个问题吗?还有其他选择吗?
答案 0 :(得分:1)
您的代码不一致。但要看到的是您首先将文件放在位图中。位图不包含exif信息。所以你扔掉了发送文件本身。不要将其转换为位图。