上传的Android应用程序的照片到谷歌云存储/应用程序引擎 - 非法字符“_” [英] Upload photos from Android app to Google Cloud Storage/App Engine - illegal character '_'
问题描述
我有从我的Android应用程序,以GCS麻烦上传照片。我可以上传文本文件,但没有照片。我已经试过各种MIME类型以及不同的Base64编码方法(DE codeBase64,EN codeBase64URLSafeString,等...)
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: 在[来源非法字符'_'(code 0x5F的)为Base64内容:N / A; 行:-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]
我看着恩codeD字符串并没有任何'_'在那里。
I looked at the encoded String and there aren't any '_' in there.
Android的code:
Android code:
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 code:
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();
}
在此先感谢。
Thanks in advance.
推荐答案
解决!
我有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
的String = Base64.en codeToString(B,Base64.DEFAULT);
String s = Base64.encodeToString(b, Base64.DEFAULT);
我试过了,但之前的形象仍然会无法正常显示。
I had tried that before but the image still wouldn't display properly.
-
我不是包装在一个ByteBuffer的字节数组,上传到GCS之前:
I wasn't wrapping the byte array in a ByteBuffer, before uploading to GCS:
GcsOutputChannel outputChannel = gcsService.createOrReplace(姓名,optionsBuilder.build());
outputChannel.write(ByteBuffer.wrap(字节));
outputChannel.close();
GcsOutputChannel outputChannel = gcsService.createOrReplace(name, optionsBuilder.build());
outputChannel.write(ByteBuffer.wrap(bytes));
outputChannel.close();
这篇关于上传的Android应用程序的照片到谷歌云存储/应用程序引擎 - 非法字符“_”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!