Я пытаюсь использовать AWS Android SDK для S3 в запущенном сервисе. Я немного новичок как с SDK, так и с сервисами. Я уверен, что у Transfer Utility также есть сервис.Android AWS S3 SDK TransferUtility не работает в службе
Handler (android.os.Handler) {11629d87} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {11629d87} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at sksDoneLater(TransferService.java:189)
at com.amazonaws.mobileconnectors.s3.transferutility.TransferService.access$200(TransferService.java:44)
at com.amazonaws.mobileconnectors.s3.transferutility.TransferService$2.handleMessage(TransferService.java:166)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Вот код, я использую, чтобы запустить его:
AmazonS3 amazonS3 = new AmazonS3Client(credentialsProvider);
mTransferUtility = new TransferUtility(amazonS3, getApplicationContext());
TransferObserver observer = mTransferUtility.upload(
S3_RAW_BUCKET_ARN,
mVidFileKey,
mVidFile);
observer.setTransferListener(new TransferListener() {...})
Линии предыдущим он говорит, что не может получить s3 клиента. Я создал клиент, как показано выше, в классе приложения и успешно использовал тот же код в действии для успешной передачи, поэтому он должен быть чем-то очевидным в отношении служб, о которых я не знаю. Вышеупомянутый код вызывается в методе, который вызывается от onStartCommand()
в сервисе.
Любая помощь будет высоко оценена.
ОБНОВЛЕНИЕ - Весь класс было предложено и показано здесь:
public class VideoCompressionService extends Service {;
private Bus bus;
private TransferUtility mTransferUtility;
private int mVidWidth;
private int mVidHeight;
private File mCompressedVidFile;
private File mVidFile;
private String mVidFileKey;
private NotificationManager mNotificationManager;
private android.support.v4.app.NotificationCompat.Builder mNotification;
public VideoCompressionService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String realPath = MediaUtils.getRealVideoPathFromURI(this.getContentResolver(), intent.getData());
if (realPath != null) {
this.mVidFile = new File(realPath);
this.mVidFileKey = intent.getStringExtra(EXTRA_VID_FILE_KEY);
this.mVidWidth = intent.getIntExtra(EXTRA_VID_WIDTH, 0);
this.mVidHeight = intent.getIntExtra(EXTRA_VID_HEIGHT, 0);
this.bus = CoPhotoApplication.getVideoCompressionBus();
if (mVidFile != null && mVidFile.exists() && mVidFile.canRead()) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new NotificationCompat.Builder(this)
.setContentTitle("Compressing Video - Step 1 of 3")
.setContentText("Uploading video for processing...")
.setSmallIcon(R.drawable.ic_launcher);
if (mVidWidth == 0 || mVidHeight == 0) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mVidFile.getAbsolutePath());
mVidWidth = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
mVidHeight = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
mmr.release();
}
uploadVidToS3();
}
return Service.START_NOT_STICKY;
} else {
VideoCompressionService.this.stopSelf();
return Service.START_NOT_STICKY;
}
} else {
VideoCompressionService.this.stopSelf();
return Service.START_NOT_STICKY;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void uploadVidToS3() {
compressionUploadStarted();
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"*********ID HERE********", // Identity Pool ID
Regions.US_EAST_1 // Region
);
AmazonS3 amazonS3 = new AmazonS3Client(credentialsProvider);
mTransferUtility = new TransferUtility(amazonS3, getApplicationContext());
TransferObserver observer = mTransferUtility.upload(
S3_RAW_BUCKET_ARN,
mVidFileKey,
mVidFile);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
if (state == TransferState.COMPLETED) {
compressionUploadFinished(true);
zencodeVideo(mVidFileKey);
} else if (state == TransferState.FAILED) {
compressionUploadFinished(false);
}
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
int progress = Math.round(100 * ((float) bytesCurrent/bytesTotal));
String progressPercentage = String.valueOf(progress) + "%";
compressionUploadProgress(progress, progressPercentage);
}
@Override
public void onError(int id, Exception ex) {
compressionUploadFinished(false);
}
});
}
private void compressionUploadStarted() {
bus.post(new CompressionUploadStartedEvent());
updateNotification();
}
private void compressionUploadProgress(int progress, String progressPercentage) {
bus.post(new CompressionUploadProgressEvent(progress, progressPercentage));
mNotification.setProgress(100, progress, false);
updateNotification();
}
private void compressionUploadFinished(boolean successfully) {
bus.post(new CompressionUploadFinishedEvent(successfully));
if (successfully) {
mNotification.setContentText("Upload complete");
} else {
mNotification.setContentTitle("Compression Failed");
mNotification.setContentText("Upload failed. Please try again later.");
}
updateNotification();
if (!successfully) {
VideoCompressionService.this.stopSelf();
}
}
private void updateNotification() {
mNotificationManager.notify(NOTIFICATION_ID, mNotification.build());
}
могли бы вы предоставить цельные класс? – Yangfan
Готово. Я редактировал вопрос. –
Хмм, я не пытался использовать TransferUtility в сервисе. Я дважды проверю, вызывает ли это проблему. Скоро вернусь к вам. – Yangfan