Android应用在等待HTTP响应时显示黑屏

时间:2012-08-02 18:53:38

标签: android http camera

我一直在尝试开发一款Android应用,让用户拍照,然后通过HTTP发送图片。我正在使用原生相机。

在用户拍摄照片并点击保存按钮后,当应用程序发送信息并等待响应时,我会看到黑屏。我宁愿显示一个progressdialog,但无论我尝试什么,黑屏都会停留在那里,只有在得到响应并点击后退按钮后才能看到progressdialog。我尝试使用setContentView()无济于事。线程用于HTTP请求。

以下是相机启动和结束的代码:

protected void startCameraActivity()
{

    File file = new File( _path );
    Uri out = Uri.fromFile( file );

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE );
    intent.putExtra( MediaStore.EXTRA_OUTPUT, out );

    startActivityForResult( intent, 0 );
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   
    switch( resultCode )
    {
    case 0:
        break;

    case -1:
        m_ProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait...", "Uploading data ...", true, true);
        onPhoto();
        break;
    }
}

protected void onPhoto()
{
    taken = true;


    PipedOutputStream pos = new PipedOutputStream();
    PipedInputStream pis = null;
    try {
        pis = new PipedInputStream(pos);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Sender sender = new Sender(pos);
    Receiver receiver = new Receiver(pis);
    sender.start();
    receiver.start();
    try {
        sender.join();
        receiver.join();
        try {
            field.setText(receiver.getIn().readUTF());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        pis.close();
        pos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:1)

点击此链接:How can I use an Async Task to upload a file to the server?

onPreExecute()中,您可以定义处理图像时要显示的内容(进度条,微调器......)。

答案 1 :(得分:0)

您可以在执行任务时使用AsyncTask并显示ProgressDialog

 private class YourTask extends AsyncTask {
        private ProgressDialog dialog;

        private GetNewsTask(Context context) {
            this.dialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage(getResources().getString(R.string.loading));
            this.dialog.show();
        }

        @Override
        protected Object doInBackground(Object... objects) {
            // your hard work
            return something;
        }

        @Override
        protected void onPostExecute(Object result) {
            if (this.dialog.isShowing()) this.dialog.dismiss();
            // hanle results

        }

    }