Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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应用程序上传照片到谷歌云存储/应用程序引擎-非法字符'_';_Java_Android_Google App Engine_Google Cloud Storage - Fatal编程技术网

Java 从Android应用程序上传照片到谷歌云存储/应用程序引擎-非法字符'_';

Java 从Android应用程序上传照片到谷歌云存储/应用程序引擎-非法字符'_';,java,android,google-app-engine,google-cloud-storage,Java,Android,Google App Engine,Google Cloud Storage,我无法将照片从android应用程序上传到GCS。我可以上传文本文件,但不能上传照片。我尝试过各种mime类型以及不同的Base64编码方法(decodeBase64、encodeBase64URLSafeString等) 我觉得我真的很接近 这是我收到的错误消息: com.google.appengine.repackage.org.codehaus.jackson.JsonParseException: [Source:N/A; 行:-1,列:-1] 我看了看编码的字符串,里面没有任何“u”

我无法将照片从android应用程序上传到GCS。我可以上传文本文件,但不能上传照片。我尝试过各种mime类型以及不同的Base64编码方法(decodeBase64、encodeBase64URLSafeString等)

我觉得我真的很接近

这是我收到的错误消息:

com.google.appengine.repackage.org.codehaus.jackson.JsonParseException: [Source:N/A; 行:-1,列:-1]

我看了看编码的字符串,里面没有任何“u”

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

}
GAE–PhotoObject:
@实体
公共类光电对象
{
公共PhotoObject(){}
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私钥;
公钥getKey()
{
返回键;
}
公共无效设置密钥(密钥)
{
this.key=key;
}
私有字符串兆字节;
公共字节[]getBytes()
{
返回decodeBytes();
}
公共空字节(字节[]MB)
{
this.mBytes=编码字节(mBytes);
}
私有字符串名称;
公共字符串getName()
{
返回名称;
}
公共void集合名(字符串名)
{
this.name=名称;
}
私有文件类型;
公共文件类型getType()
{
返回类型;
}
公共void集合类型(文件类型)
{
this.type=type;
}
/**
* 
*@see#getBytes()
*@return Base64解码值或{@code null}表示无
* 
*@从1.14开始
*/
公共字节[]解码字节()
{
返回com.google.api.client.util.Base64.decodeBase64(MB);
}
/**
* 
*@see#setBytes()
* 
*
*该值编码为Base64或{@code null}表示无。
*

* *@从1.14开始 */ 公共字符串编码字节(字节[]字节) { //this.mBytes=com.google.api.client.util.Base64.encodeBase64URLSafeString(字节); this.mBytes=com.google.api.client.util.Base64.encodeBase64String(字节); 返回这个.mBytes; } } GAE-insertPhotoObject: 尝试 { 最终GcsService GcsService=GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); GcsFilename=新的GcsFilename(“testbucket123”,文件名); GcsFileOptions.Builder options Builder=新的GcsFileOptions.Builder(); optionsBuilder.mimeType(“image/jpg”); GcsOutputChannel outputChannel=gcsService.createOrReplace(名称,optionsBuilder.build()); ObjectOutputStream out=新的ObjectOutputStream(Channels.newOutputStream(outputChannel)); out.write(字节); out.close(); }
提前感谢。

已解决

我有两个问题

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

    字符串s=Base64.encodeToString(b,Base64.DEFAULT)

  • 我以前试过,但图像仍然无法正确显示

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

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


  • 我在互联网上感到孤独,直到我找到了你的贴子,非常感谢。似乎生成的端点模型的方法
    encodeXXX
    内部使用
    com.google.api.client.util.Base64.encodebase64urlsafesting(byteData)
    以某种方式创建无效的Base64内容,看起来你在使用android.util.Base64。如果Java 7使用DatatypeConverter.printBase64Binary(字节)。Java8甚至有Java.util.Base64