将图像从Android上传到Google云存储:java.lang.NullPointerException
我正在尝试从我的android客户端应用程序上传一个图像到Google Bucket存储,我使用以下代码 但是,当我尝试发布图像时,会出现以下错误: java.lang.NullPointerException:尝试调用虚拟方法“void” com.google.api.client.json.jsonggenerator.serialize(java.lang.Object)' 关于空对象引用 代码:将图像从Android上传到Google云存储:java.lang.NullPointerException,java,android,json,rest,google-app-engine,Java,Android,Json,Rest,Google App Engine,我正在尝试从我的android客户端应用程序上传一个图像到Google Bucket存储,我使用以下代码 但是,当我尝试发布图像时,会出现以下错误: java.lang.NullPointerException:尝试调用虚拟方法“void” com.google.api.client.json.jsonggenerator.serialize(java.lang.Object)' 关于空对象引用 代码: try { com.google.api.client.http.javanet.N
try {
com.google.api.client.http.javanet.NetHttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
AssetManager am = PublishView.this.getAssets();
InputStream inputStream = am.open("key.p12"); //key from service account
//convert key into class File. from inputstream to file. in an aux class.
File file = stream2file(inputStream);
JsonFactory JSON_FACTORY = new JsonFactory() {
@Override
public JsonParser createJsonParser(InputStream in) throws IOException {
return null;
}
@Override
public JsonParser createJsonParser(InputStream in, Charset charset) throws IOException {
return null;
}
@Override
public JsonParser createJsonParser(String value) throws IOException {
return null;
}
@Override
public JsonParser createJsonParser(Reader reader) throws IOException {
return null;
}
@Override
public JsonGenerator createJsonGenerator(OutputStream out, Charset enc) throws IOException {
return null;
}
@Override
public JsonGenerator createJsonGenerator(Writer writer) throws IOException {
return null;
}
};
String STORAGE_SCOPE = "https://www.google.com/analytics/feeds/";
//Google Credentianls
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("MY_SERVICE_NAME@MY_SERVICE_ACC_ID.iam.gserviceaccount.com")
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(file)
.build();
String URI = "https://storage.googleapis.com/" + "BUCKET_NAME" + "/" + "test" + ".jpg";
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
//byte array holds the data, in this case the image i want to upload in bytes.
Resources res = getResources();
Drawable drawable = res.getDrawable(R.mipmap.ic_launcher);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
HttpContent contentsend = new ByteArrayContent("image/jpeg", byteArray);
HttpRequest putRequest = requestFactory.buildPutRequest(url, contentsend);
com.google.api.client.http.HttpResponse response = putRequest.execute();
String content = response.parseAsString();
Log.d("debug", "response is:" + response.getStatusCode());
Log.d("debug", "response content is:" + content);
} catch (Exception ex) {
}
有人能指出我做错了什么吗