1
我的圖像存儲在雲端,但我生成的鏈接不是公共鏈接。我想再次從URL訪問圖片,但不能。如何在Android中的Google雲端存儲中創建存儲圖像的公共鏈接
我已存儲的圖像雲存儲與下面的代碼:
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("[email protected]")
.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);