上传图片从安卓到GCS

上传图片从安卓到GCS

本文介绍了上传图片从安卓到GCS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android的图片直接上传到谷歌云存储。但是API似乎不工作。他们有一些Java的样本,被连接到App引擎。我不认为这是证明工作在Android上的任何样本。

在Android上,我尝试使用JSON API来上传图片。我可以上传图像对象,但它似乎已损坏。此外生成认证令牌似乎棘手了。

我现在打这个权利。有没有人在这个地球上曾经试图从上传的Andr​​oid使用Java客户端或JSON API,并成功图像/视频?有人能指出我朝着正确的方向吧。它已经与来自谷歌这个存储API非常令人失望的经验。请分享您的经验,如果有人做了。下面是code,我从Android的尝试,而尝试使用GCS的JSON API。

 私有静态字符串uploadFile(富媒体广告媒体){
    DefaultHttpClient客户端=新DefaultHttpClient();
    点阵位图= BitmapUtils.getBitmap(media.getLocalUrl());
    HttpPost后=新HttpPost(GCS_ROOT + media.getLocalUrl()+_+ System.currentTimeMillis的());
    如果(media.getType()== RichMedia.RichMediaType.PICTURE){
        post.setHeader(内容类型,图像/ JPEG);
    } 其他 {
        post.setHeader(内容类型,视频/ MP4);
    }
    post.setHeader(授权,AIzaSyCzdmCMwiMzl6LD7R2obF0xSgnnx5rEfeI);
    //post.setHeader("Content-Length,将String.valueOf(bitmap.getByteCount()));
    ByteArrayOutputStream流=新ByteArrayOutputStream();
    bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,流);
    字节[]的字节数组= stream.toByteArray();

    尝试 {
        post.setEntity(新StringEntity(新GSON()的toJSON(字节阵列)的ToString())。);
        HTT presponse响应= client.execute(后);
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
        字符串eachLine = NULL;
        StringBuilder的建设者=新的StringBuilder();
        而((eachLine = reader.readLine())!= NULL){
            builder.append(eachLine);
        }
        L.d(响应=+ builder.toString());
        的JSONObject对象=新的JSONObject(builder.toString());
        字符串名称= object.getString(姓名);
        返回名称;
    }赶上(IOException异常E){
        L.print(E);
    }赶上(JSONException E){
        L.print(E);
    }
    返回null;
}
 

我遇到了两个问题。

  1. 这得到了上传到服务器上的文件已损坏。这并不是说我上传了相同的图像。这是currupt。

  2. 授权密钥过期非常频繁。就我而言,我使用的由gsutil产生的AUTH code。

解决方案

由于没有人回答这个问题之前,让我更新我解决了这个问题的方式。我结束了下面这个https://github.com/pliablematter/simple-cloud-storage项目。

我可以从我的Andr​​oid应用程序上传图片/视频到GCS。

I am trying to upload an image from Android directly to Google cloud storage. But the API does not seem to work. They have some Java samples which are tied to the App engine. I don't see any sample which is proven to work on Android.

On Android, I tried using the json api to upload an image. I am able to upload an image object but it seems to be corrupt. Moreover generating the authentication token seems to be tricky too.

I am struck with this right now. Did anyone on this earth ever tried uploading an image/video from Android using Java client or Json API and succeeded? Can someone point me in the right direction please. It has been very disappointing experience with this Storage api from Google. Please share your experiences if someone did it.Below is the code that I am trying from Android while trying to use the GCS's JSON API.

private static String uploadFile(RichMedia media) {
    DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = BitmapUtils.getBitmap(media.getLocalUrl());
    HttpPost post = new HttpPost(GCS_ROOT + media.getLocalUrl() + "_" + System.currentTimeMillis());
    if(media.getType() == RichMedia.RichMediaType.PICTURE) {
        post.setHeader("Content-Type", "image/jpeg");
    } else {
        post.setHeader("Content-Type", "video/mp4");
    }
    post.setHeader("Authorization", "AIzaSyCzdmCMwiMzl6LD7R2obF0xSgnnx5rEfeI");
    //post.setHeader("Content-Length", String.valueOf(bitmap.getByteCount()));
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    try {
        post.setEntity(new StringEntity(new Gson().toJson(byteArray).toString()));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String eachLine = null;
        StringBuilder builder = new StringBuilder();
        while ((eachLine = reader.readLine()) != null) {
            builder.append(eachLine);
        }
        L.d("response = " + builder.toString());
        JSONObject object = new JSONObject(builder.toString());
        String name = object.getString("name");
        return  name;
    } catch (IOException e) {
        L.print(e);
    } catch (JSONException e) {
        L.print(e);
    }
    return null;
}

I am running into two issues here.

  1. The file which got uploaded to the server is corrupt. It is not the same image that I uploaded. It is currupt.

  2. The authorization key expires very often. In my case, I am using the auth code generated by gsutil.

解决方案

Since no one is answering this question, let me update the way I solved this problem. I ended up following this https://github.com/pliablematter/simple-cloud-storage project.

I could upload Pictures/Videos to GCS from my Android app.

这篇关于上传图片从安卓到GCS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:00