没有互联网连接时排队图像路径

时间:2013-11-20 11:21:05

标签: java android android-asynctask queue sharedpreferences

我很难跟踪我的队列。 我正在尝试将图像路径存储到队列中,这样我就可以使用队列在上网时开始上传我的图像(稍后)。上传图片是一个asynctask,在postExecute我试图发送一封邮件,上传的图片附在另一个asynctask上。

这是我的UploadImage AsyncTask。我认为我做得太难了,而且比现在更容易完成。

private class UploadImageTask extends AsyncTask<Void, Void, Integer> {
    ProgressDialog dialog;

    /**
     * Private integer which counts how many times we've tried to upload the
     * Image.
     */
    private int _counter = 0;

    private List<String> imageUploadList = new ArrayList<String>();

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(AppStatus.haveNetworkConnection(_context)){
            if(isPhotoTaken()){
                dialog = new ProgressDialog(Step4.this);
                dialog.setCancelable(false);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setMessage(getString(R.string.uploadingMessage));
                dialog.setTitle(getString(R.string.uploadingTitle));
                dialog.show();
            }
        }
    }

    protected Integer doInBackground(Void... params) {
        init();
        postData();
        return null;
    }
    public void init(){
        _counter = 0;
        _beenHere = true;
        for(String path : imageUploadList){
            Debug.out("Path: "+path);
        }
    }
    public void postData() {
        if (isPhotoTaken()) {
            if(AppStatus.haveNetworkConnection(_context)){
                if(_beenHere){
                    ImageUploader.uploadFile(getPhotoPath(),
                            "http://obo.nl/android-upload-image.php", Step4.this);
                } else {
                    for(String path : imageUploadList){
                        Debug.out(path);
                        ImageUploader.uploadFile(path,
                                "http://obo.nl/android-upload-image.php", Step4.this);
                    }
                }
            } else {
                if (_counter == 0) {
                    _counter++;
                    _activity.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(_context,
                                    getString(R.string.noInternetImageNotUploaded),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                    imageUploadList.add(getPhotoPath());
                }
                try {
                    if(_beenHere){
                        _beenHere = false;
                        goToNextIntent();
                    }
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }               
                postData();
            }
        }
    }

    private void goToNextIntent(){
        Intent intent = new Intent(Step4.this, Step5.class);
        intent.putExtra(EXTRA_MESSAGE, (Serializable) _user);
        intent.putExtra(EXTRA_MESSAGE2, _isRepairable);
        intent.putExtra(EXTRA_MESSAGE3, _injury);
        intent.putExtra(EXTRA_MESSAGE4, _category);
        intent.putExtra(EXTRA_MESSAGE5, _inch);
        intent.putExtra(EXTRA_MESSAGE6, _size);
        startActivity(intent);
    }
    protected void onPostExecute(Integer result) {
        if(isPhotoTaken()){
            if(dialog != null){
                dialog.dismiss();
            }
        }
        mailing(_isRepairable);
        new MyAsyncTask().execute(_mail);
    }
}

该行:

if(AppStatus.haveNetworkConnection(_context))
如果用户具有有效的Internet连接,则

返回布尔值true。否则就是假的。

我想要的是将所有图像路径(以及之后发送的邮件)排队到所需的ArrayList中,这样我就可以在用户具有正常工作的互联网连接时将它们全部发送出去。请帮帮我!

0 个答案:

没有答案