【发布时间】:2017-01-06 14:37:48
【问题描述】:
我的图像正在存储在云端,但我生成的链接不是公共链接。我想再次从 URL 访问图像但不能。
我已使用以下代码将图像存储在云存储中:
List<String> scopes = new ArrayList<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
AssetManager am = getAssets();
InputStream inputStream = am.open("Alternew-e82795616c8c.p12"); //you should not put the key in assets in prod version.
//convert key into class File. from inputstream to file. in an aux class.
File file = stream2file(inputStream);
//Google Credentianls
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("alternew-152712@appspot.gserviceaccount.com")
.setServiceAccountScopes((scopes))
.setServiceAccountPrivateKeyFromP12File(file)
.build();
String URI = "https://storage.googleapis.com/alter_post_images/" + "post" + timeStamp + "_" + ImageCount + "." + finalExtention;
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
finalPostUrlForSave = URI;
url = new GenericUrl(URI);
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
bitMapData = buffer.array();
HttpContent contentsend = new ByteArrayContent("image/" + finalExtention, bitMapData);
putRequest = requestFactory.buildPutRequest(url, contentsend);
com.google.api.client.http.HttpResponse response = putRequest.execute();
String content = response.parseAsString();
Log.e("debug", "response is:" + response.getStatusCode());
Log.e("debug", "response content is:" + content);
【问题讨论】:
标签: android google-cloud-storage