2017-10-13 13 views
0

私はAndroidアプリを書くのに非常に緑色です。 AWS S3に画像をアップロードするためのサンプルコードをいくつかコピーします。しかし、私は電話で実行すると、常にアプリケーションを閉じます。以下のコード:AWS S3に画像をアップロードするエラー

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String mediaFile = "IMG_" + timeStamp + ".jpg"; 

      File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 

      File tmpFile = new File(dir, mediaFile); 

      final Uri outputFileUri = Uri.fromFile(tmpFile); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(intent, PICK_FROM_CAMERA); 

      String ACCESS_KEY="ABCDEFGHIJKLMNOPQ", 
        SECRET_KEY="S123T456I789", 
        MY_BUCKET="Photo", 

      AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY); 
      AmazonS3 s3 = new AmazonS3Client(credentials); 

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

TransferObserver observer = transferUtility.upload(MY_BUCKET,mediaFile,file); 

多くのおかげで エドモンド

+0

https://stackoverflow.com/a/34872276/3615605これを試してみてください – MathaN

答えて

0

私はプールIdでS3のアップロードを使用することをお勧めします。アプリでハードコードされたSECRET_KEYとACCESS_KEYを使用することはお勧めできません。 転送ユーティリティもプールIDと同じ方法で動作します。資格プロバイダを変更するだけで済みます。アプリの

public static TransferUtility getTransferUtility(Context context) { 
    if (sTransferUtility == null) { 
     sTransferUtility = new TransferUtility(getS3Client(context.getApplicationContext()), 
       context.getApplicationContext()); 
    } 
    return sTransferUtility; 
} 

public static AmazonS3Client getS3Client(Context context) { 
    if (sS3Client == null) { 
     sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext())); 
     sS3Client.setRegion(Region.getRegion(Regions.AP_SOUTH_1)); 
    } 
    return sS3Client; 
} 

private static CognitoCachingCredentialsProvider getCredProvider(Context context) { 
    if (sCredProvider == null) { 
     sCredProvider = new CognitoCachingCredentialsProvider(
       context.getApplicationContext(), 
       Constants.COGNITO_POOL_ID, 
       Regions.AP_NORTHEAST_1); 
    } 
    return sCredProvider; 
} 

の地域やプールイドおよびアップロードする転送ユーティリティを使用します:コードは、以下を参照してください

TransferUtility transferUtility = AppUtil.getTransferUtility(context); 
TransferObserver transferObserver = transferUtility.upload(BUCKET_NAME, forKey, new File(fileUrl)); 
    transferObserver.setTransferListener(new TransferListener() { 
    @Override 
    public void onStateChanged(int id, TransferState state) { 
     if (state == TransferState.COMPLETED) { 
      // Completed 
     } 
    } 

    @Override 
    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {} 

    @Override 
    public void onError(int id, Exception ex) { 
     ex.printStackTrace(); 
     // Error 
    } 
}); 
関連する問題