0

我一直在尝试开发一个 Android 应用程序,它可以让用户拍照,然后通过 HTTP 发送图像。我正在使用本机相机。

用户拍照并点击保存按钮后,我在应用程序发送信息并等待响应时出现黑屏。我宁愿显示一个进度对话框,但无论我尝试什么,黑屏都会停留在那里,并且只有在获得响应并点击后退按钮后才能看到进度对话框。我尝试使用 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();
    }

}
4

2 回答 2

1

查看此链接:如何使用异步任务将文件上传到服务器?

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

于 2012-08-02T19:02:09.907 回答
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

        }

    }
于 2012-08-02T19:29:37.437 回答