Android AWS S3 SDK TransferUtility不在服务中 [英] Android AWS S3 SDK TransferUtility Not Working in Service

查看:105
本文介绍了Android AWS S3 SDK TransferUtility不在服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在已启动的服务中使用适用于S3的AWS开发工具包.我对SDK和服务都有些陌生.我非常有信心Transfer Utility也正在运行服务.

I am trying to use the AWS Android SDK for S3 in a started service. I am a little new to both the SDK and Services. I am pretty confident that the Transfer Utility is also running a service.

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)

这是我用来启动它的代码:

Here is the code I am using to start it up:

    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()调用的方法中调用了上面的代码.

The line previous it says it can't get s3 client. I created the client just as shown above in application class and successfully used the same code in an activity to perform a successful transfer so it must be something obvious about services that I am ignorant of. The above code is called in a method that is called from onStartCommand() in the service.

任何帮助将不胜感激.

更新-请求整个类,并在此处显示:

UPDATE - Whole Class was requested and is shown here:

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

推荐答案

我发现自己也遇到了同样的问题... 这就是为我解决的问题:

I found myself in the same issue... Here it is what solved for me:

我注意到我在清单中注册了SyncAdapter的以下服务:

I noticed I had the following service for my SyncAdapter registered in Manifest:

        <service
        android:name=".sync.SyncService"
        android:exported="true"
        android:process=":sync">
        <intent-filter>
            <action android:name="android.content.SyncAdapter"/>
        </intent-filter>
        <meta-data android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>

android:process =:sync"告诉服务在名为:sync的私有进程中运行 因此,我只是告诉Amazon SDK中的TransferService可以在同一线程中运行.

The android:process=":sync" tells service to run in a private process called :sync So I just told the TransferService from Amazon SDK to run in this same thread.

        <service
        android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
        android:process=":sync"
        android:enabled="true" />

此后,我再也没有出现错误 TransferService无法获取s3客户端,它将停止,最后终于可以将我的图片上传到S3

After that, I no more got the error TransferService can't get s3 client, and it will stop, and finally was able to upload my pictures to S3

这篇关于Android AWS S3 SDK TransferUtility不在服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆