我的图片存储在云端,但我生成的链接不是公共链接。我想再次从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);
答案 0 :(得分:0)
我找到了回答!!!
//to create file name
//take the current timeStamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "Image_business_profile" + timeStamp + "." + finalExtention;
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://xxx.appspot.com/images");
// Create a reference to "mountains.jpg"
StorageReference mountainsRef = storageRef.child(imageName);
// Create a reference to 'images/mountains.jpg'
StorageReference mountainImagesRef = storageRef.child("images/" + imageName);
// byte[] data = CreateByteArray();
UploadTask uploadTask;
if (finalExtention.equalsIgnoreCase("gif")) {
// Create the file metadata
Uri file = Uri.fromFile(new File(finalimageName));
StorageReference riversRef = storageRef.child("images/" + FileName);
uploadTask = riversRef.putFile(file);
} else {
byte[] data = CreateByteArray();
uploadTask = mountainsRef.putBytes(data);
}
//UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.e("downloadUrl", "downloadUrl " + downloadUrl);
FinalUrl = downloadUrl.toString();
MoveToNextScreen();
}
});