【问题标题】:problem with uploading the image to url in android在android中将图像上传到url的问题
【发布时间】:2011-08-01 16:46:36
【问题描述】:

在我的应用程序中,我正在尝试捕获图像并且我想将其发送到一个 url,但问题是当活动开始并打开相机时,在我捕获图像之前上传到服务器部分得到已启动,因此图像未上传。

以下是我的代码,请帮我解决问题...

public class Camera extends Activity
{
    protected static final int TAKE_RECEIPT = 0;
    private Uri imageCaptureUri;
    private String filename;
    private Runnable submitReceiptRunnable = new Runnable()
    {
        public void run()
        {
            publishReceipt();
        }

        private void publishReceipt()
        {

        }
    };

    private ProgressDialog progressDialog;
    OutputStream outStream;

    Intent myIntent;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

        takePictureFromCamera();
    }
            private void takePictureFromCamera()
            {
                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    imageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "tmp_receipt_"
                            + String.valueOf(System.currentTimeMillis()) + ".jpg"));

                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, TAKE_RECEIPT);

                    String path =  String.format("/sdcard/%d.jpg",System.currentTimeMillis());
                    try
                    {
                        outStream = new FileOutputStream(path);
                        doFileUpload(path);
                        Log.e("Camera",""+outStream);
                    }
                    catch (FileNotFoundException e)
                    {
                        e.printStackTrace();
                    }


            }

            private void takeReceiptCallback(int resultCode, Intent data)
            {
                if (resultCode == Activity.RESULT_OK)
                {
                    submitReceipt();
                }
            }

            private void submitReceipt()
            {
                Thread thread = new Thread(null, submitReceiptRunnable);
                thread.start();
//                progressDialog = ProgressDialog.show(this, "Please wait...", "Publishing receipt ...", true);               
            }

            private String getBase64Receipt() {
                try {
                    InputStream inputStream = getContentResolver().openInputStream(imageCaptureUri);
//                    byte[] bytes = CommonUtil.getBytesFromInputStream(inputStream);
//                    return Base64.encodeBytes(bytes);//(selectedImage.getPath().getBytes());    
                }
                catch (IOException e)
                {
                    Log.e("getbase64Reciept", ""+e);
                }

                return null;
            }

            private void publishReceipt()
            {
                String receipt = getBase64Receipt();
            }



            private void doFileUpload(String exsistingFileName)
            {
                  HttpURLConnection conn = null;
                  DataOutputStream dos = null;
                  DataInputStream inStream = null;

                  String lineEnd = "\r\n";
                  String twoHyphens = "--";
                  String boundary =  "*****";


                  int bytesRead, bytesAvailable, bufferSize;

                  byte[] buffer;

                  int maxBufferSize = 1*1024*1024;

                  String responseFromServer = "";

                  String urlString = "http://demo.great.com/spot/upload.php";

                  try
                  {
                   //------------------ CLIENT REQUEST

                  Log.e("MediaPlayer","Inside second Method");

                  FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));

                  URL url = new URL(urlString);

                  conn = (HttpURLConnection) url.openConnection();

                  // Allow Inputs
                  conn.setDoInput(true);

                  // Allow Outputs
                  conn.setDoOutput(true);

                  // Don't use a cached copy.
                  conn.setUseCaches(false);

                  // Use a post method.
                  conn.setRequestMethod("POST");

                  conn.setRequestProperty("Connection", "Keep-Alive");

                  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

                  dos = new DataOutputStream(conn.getOutputStream() );

                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
                  dos.writeBytes(lineEnd);

                  Log.e("MediaPlayer","Headers are written");

                  // create a buffer of maximum size
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];

                  // read file and write it into form...
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                  while (bytesRead > 0)
                  {
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  }

                  // send multipart form data necesssary after file data...

                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                   // close streams
                   Log.e("MediaPlayer","File is written");
                   fileInputStream.close();
                   dos.flush();
                   dos.close();

                  }
                  catch (MalformedURLException ex)
                  {
                       Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
                  }

                  catch (IOException ioe)
                  {
                       Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
                  }

                  //------------------ read the SERVER RESPONSE

                  try {
                        inStream = new DataInputStream ( conn.getInputStream() );
                        String str;

                        while (( str = inStream.readLine()) != null)
                        {
                             Log.e("MediaPlayer","Server Response"+str);
                        }
                        inStream.close();

                  }
                  catch (IOException ioex){
                       Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
                  }       
            }

【问题讨论】:

    标签: android url upload camera


    【解决方案1】:

    您没有从

    获取结果的函数
    startActivityForResult(intent, TAKE_RECEIPT);    
    

    你需要实现这个方法:

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-03
      • 2017-11-21
      • 2012-06-10
      • 1970-01-01
      • 2019-01-24
      • 2011-07-30
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多