【问题标题】:How to create public link of stored image in Google Cloud storage in Android如何在 Android 的 Google Cloud 存储中创建存储图像的公共链接
【发布时间】: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


    【解决方案1】:

    我找到答案了!!!

     //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();
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2019-01-10
      • 2021-11-29
      相关资源
      最近更新 更多