Upload photos from Android app to Google Cloud Storage/App Engine - illegal character #39;_#39;(将照片从 Android 应用上传到 Google Cloud Storage/App Engine - 非法字符“_)
问题描述
我在将照片从我的 Android 应用程序上传到 GCS 时遇到问题.我可以上传文本文件,但不能上传照片.我尝试了各种 mime 类型以及不同的 Base64 编码方法(decodeBase64、encodeBase64URLSafeString 等...)
I've having trouble uploading photos from my android app to GCS. I'm able to upload text files but not photos. I've tried various mime-types as well as different Base64 encoding methods (decodeBase64, encodeBase64URLSafeString, etc...)
我觉得我真的很亲近.
这是我收到的错误消息:
This is the error message that I receive:
com.google.appengine.repackaged.org.codehaus.jackson.JsonParseException:[来源:N/A;base64 内容中的非法字符_"(代码 0x5f)行:-1,列:-1]
com.google.appengine.repackaged.org.codehaus.jackson.JsonParseException: Illegal character '_' (code 0x5f) in base64 content at [Source: N/A; line: -1, column: -1]
我查看了编码后的字符串,里面没有任何_".
I looked at the encoded String and there aren't any '_' in there.
安卓代码:
Activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 0:
{
try
{
InputStream is = getContentResolver().openInputStream(data.getData());
tvMessage.setText("Done!");
byte[] b = getBytes(is);
gaeTask task = new gaeTask();
PhotoObject p = new PhotoObject();
p.encodeBytes(b);
p.setName("picturejpg.jpg");
task.execute(p);
}
}
AsnycTask:
protected String doInBackground(PhotoObject... params)
{
String responseMessage = "";
try
{
PhotoObjectEndpoint builder = new PhotoObjectEndpoint(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), new HttpRequestInitializer()
{
@Override
public void initialize(HttpRequest arg0) throws IOException
{
// TODO Auto-generated method stub
}
});
PhotoObject p = params[0];
builder.insertPhotoObject(p).execute();
responseMessage = p.getName() + " was successfully deployed.";
}
GAE/GCS 代码:
GAE/GCS Code:
GAE – PhotoObject:
@Entity
public class PhotoObject
{
public PhotoObject(){}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
public Key getKey()
{
return key;
}
public void setKey(Key key)
{
this.key = key;
}
private String mBytes;
public byte[] getBytes()
{
return decodeBytes();
}
public void setBytes(byte[] mBytes)
{
this.mBytes = encodeBytes(mBytes);
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
private FileType type;
public FileType getType()
{
return type;
}
public void setType(FileType type)
{
this.type = type;
}
/**
*
* @see #getBytes()
* @return Base64 decoded value or {@code null} for none
*
* @since 1.14
*/
public byte[] decodeBytes()
{
return com.google.api.client.util.Base64.decodeBase64(mBytes);
}
/**
*
* @see #setBytes()
*
* <p>
* The value is encoded Base64 or {@code null} for none.
* </p>
*
* @since 1.14
*/
public String encodeBytes(byte[] bytes)
{
//this.mBytes = com.google.api.client.util.Base64.encodeBase64URLSafeString(bytes);
this.mBytes = com.google.api.client.util.Base64.encodeBase64String(bytes);
return this.mBytes;
}
}
GAE - insertPhotoObject:
try
{
final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
GcsFilename name = new GcsFilename("testbucket123", fileName);
GcsFileOptions.Builder optionsBuilder = new GcsFileOptions.Builder();
optionsBuilder.mimeType("image/jpg");
GcsOutputChannel outputChannel = gcsService.createOrReplace(name, optionsBuilder.build());
ObjectOutputStream out = new ObjectOutputStream(Channels.newOutputStream(outputChannel));
out.write(bytes);
out.close();
}
提前致谢.
推荐答案
已解决!
我有 2 个问题.
我的字符串编码不正确.当我实际查看输出的 JSON 时,我可以看到字符串中有_".我通过添加行来解决这个问题
I was encoding the string improperly. When I actually looked at the outputted JSON, I could see the there were "_" in the string. I fixed this by using adding the line
字符串 s = Base64.encodeToString(b, Base64.DEFAULT);
String s = Base64.encodeToString(b, Base64.DEFAULT);
我之前尝试过,但图像仍然无法正常显示.
I had tried that before but the image still wouldn't display properly.
在上传到 GCS 之前,我没有将字节数组包装在 ByteBuffer 中:
I wasn't wrapping the byte array in a ByteBuffer, before uploading to GCS:
GcsOutputChannel outputChannel = gcsService.createOrReplace(name, optionsBuilder.build());
outputChannel.write(ByteBuffer.wrap(bytes));outputChannel.close();
GcsOutputChannel outputChannel = gcsService.createOrReplace(name, optionsBuilder.build());
outputChannel.write(ByteBuffer.wrap(bytes));
outputChannel.close();
这篇关于将照片从 Android 应用上传到 Google Cloud Storage/App Engine - 非法字符“_"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将照片从 Android 应用上传到 Google Cloud Storage/App Engine - 非法字符“_"
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01