2017-07-18 156 views
0
//getting access to the photo gallery 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) { 
     Uri selectedImage = data.getData(); 
     uploadImage.setImageURI(selectedImage); 
    } 
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show(); 

} 


//Toast.makeText(this, "Test", Toast.LENGTH_LONG).show(); 
//Trying to upload image to the AWS S3bucket- 
public void uploadImageToAWS(View v) { 
//String sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "Camera/"; 
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show(); 
    File MY_FILE = new File("myPhoto.png"); 
    String YOUR_IDENTITY_POOL_ID ="POOL_ID"; 
    CognitoCachingCredentialsProvider credentialsProvider = 
      new CognitoCachingCredentialsProvider(getApplicationContext(), 
        YOUR_IDENTITY_POOL_ID, 
        Regions.US_EAST_1); 


    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider); 
    s3Client.setRegion(Region.getRegion(Regions.US_EAST_1)); 

    TransferUtility transferUtility = new TransferUtility(s3Client, 
      getApplicationContext()); 

    TransferObserver observer = transferUtility.upload(
      "apptestingbucket",   // The S3 bucket to upload to 
      FILE_NAME, // The key for the uploaded object 
      MY_FILE);// The location of the file to 
    observer.setTransferListener(new TransferListener() { 

     @Override 
     public void onStateChanged(int id, TransferState state) { 
      if(state == TransferState.COMPLETED){ 
       Toast.makeText(Social.this, "transfer Succeded!", Toast.LENGTH_LONG).show(); 
      } 

     } 

     @Override 
     public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 
      int percentage = (int) (bytesCurrent/bytesTotal * 100); 
      Log.e("Status", "Percentage" + percentage); 

     } 

     @Override 
     public void onError(int id, Exception ex) { 
      Log.e("MY_BUCKET", "Error: " + ex.getMessage()); 

     } 
    }); 




     } 



private void retrieveImageFromAWS(){ 
    File sdcard = Environment.getExternalStorageDirectory(); 
    File MY_FILE = new File(sdcard, "social.png"); 
    String YOUR_IDENTITY_POOL_ID ="POOL_ID"; 
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(getApplicationContext(), 
      YOUR_IDENTITY_POOL_ID 
      , Regions.US_EAST_1); 
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider); 
    TransferUtility transferUtility = new TransferUtility(s3Client, 
      getApplicationContext()); 
    TransferObserver observer = transferUtility.download("BUCKET_NAME", 
      "FILENAME_TO_BE_DOWNLOADED", MY_FILE); 

    observer.setTransferListener(new TransferListener() { 
     @Override 
     public void onStateChanged(int id, TransferState state) { 
      if(state == TransferState.COMPLETED){ 
       Toast.makeText(Social.this, "Retrieve completed", Toast.LENGTH_LONG).show(); 
      } 

     } 

     @Override 
     public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 
      int percentage = (int) (bytesCurrent/bytesTotal * 100); 
      Log.e("status", "percentage" +percentage); 

     } 

     @Override 
     public void onError(int id, Exception ex) { 
      Log.e("MY_BUCKET", "Error Downloading: " + ex.getMessage()); 


     } 
    }); 


} 

這是我基於Android s3文檔,我試圖從照片庫中選取圖片或從手機拍照並將其上傳到S3存儲桶並將其恢復到應用界面。但是,如果我運行程序,我的應用程序就會崩潰。我打電話如何將圖片上傳到AWS s3存儲桶?

uploadImageToAWS(v) 

insede的

BtnUpload.onClicklistener 

任何例子或者一些幫助,將不勝感激。謝謝!

+0

1)如果程序崩潰,請添加logcat 2)您可能需要一個AsyncTask來執行任何網絡操作 –

回答

0

嘗試檢查您的Android Manifests文件中是否包含使用權限Internet。如果這不起作用,請嘗試查看是否可以將圖像上傳到您的應用程序之外(例如,通過命令行)到您的存儲桶。也許有某種安全設置你沒有正確設置。確保您可以ping您的服務器並正確設置您的安全設置。

相關問題