2017-01-06 47 views
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); 

回答

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(); 
     } 
    }); 
相關問題