2017-10-13 3 views
0

Ich bin sehr grün für Android Apps schreiben. Ich kopiere einen Beispielcode, um das Bild in AWS S3 hochzuladen. Aber wenn ich telefoniere, schließe es immer die Apps. Der Code unten:Bild auf AWS S3 hochladen Fehler

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); 

Vielen Dank Edmond

+0

https://stackoverflow.com/a/34872276/3615605 Versuchen Sie, diese – MathaN

Antwort

0

Ich empfehle S3 Upload von Pool-ID zu verwenden. Die Verwendung von SECRET_KEY und ACCESS_KEY, die in der App fest codiert sind, wird nicht empfohlen. Transfer-Dienstprogramm funktioniert auf die gleiche Weise mit Pool-ID auch Sie müssen nur den Credential Provider ändern. Siehe unten stehende Code:

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; 
} 

Ändern der Region und Pool-ID für Ihre Anwendung und Nutzung Transfer Utility zum Hochladen:

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 
    } 
}); 
Verwandte Themen