繁体   English   中英

将Android应用中的照片上传到Google云端存储/应用引擎 - 非法字符'_'

[英]Upload photos from Android app to Google Cloud Storage/App Engine - illegal character '_'

我在将Android应用中的照片上传到GCS时遇到问题。 我可以上传文本文件而不上传照片。 我尝试了各种mime类型以及不同的Base64编码方法(decodeBase64,encodeBase64URLSafeString等...)

我觉得我真的很亲密。

这是我收到的错误消息:

com.google.appengine.repackaged.org.codehaus.jackson.JsonParseException:base64内容中的非法字符'_'(代码0x5f)[来源:N / A; line:-1,column:-1]

我看了编码的字符串,那里没有任何'_'。

Android代码:

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 – 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();

}

提前致谢。

解决!

我有两个问题。

  1. 我不正确地编码字符串。 当我实际查看输出的JSON时,我可以看到字符串中有“_”。 我通过添加线来修复此问题

    String s = Base64.encodeToString(b,Base64.DEFAULT);

我之前尝试过,但图像仍无法正常显示。

  1. 在上传到GCS之前,我没有将字节数组包装在ByteBuffer中:

    GcsOutputChannel outputChannel = gcsService.createOrReplace(name,optionsBuilder.build());
    outputChannel.write(ByteBuffer.wrap(字节)); outputChannel.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM