如何从Android将图像发送到服务器ASP.NET

时间:2014-08-21 14:11:33

标签: android multithreading android-asynctask

我想通过按钮clic向服务器发送一个标志。

在我的类CaptureSignature中:

public class CaptureSignature extends ActionBarActivity{

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    ...
 mGetSign = (Button)findViewById(R.id.getsign);
...
 mGetSign.setOnClickListener(new OnClickListener() 
            {        
                public void onClick(View v) 
                {
                    Log.v("log_tag", "Panel Saved");
                    boolean error = captureSignature();
                    if(!error){
                        mView.setDrawingCacheEnabled(true);
                        mSignature.save(mView);
                        Bundle b = new Bundle();
                        b.putString("status", "done");
                        Intent intent = new Intent();
                        intent.putExtras(b);
                        setResult(RESULT_OK,intent);   
                        finish();
                    }
                }
            });
.....

 public void save(View v) 
        {
            Log.v("log_tag", "Width: " + v.getWidth());
            Log.v("log_tag", "Height: " + v.getHeight());
            if(mBitmap == null)
            {
                mBitmap =  Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);
            }
            Canvas canvas = new Canvas(mBitmap);
            try
            {
                 current = yourName.getText().toString()+"_"+uniqueId + ".png";  
                 mypath= new File(tempDir,current);             

                FileOutputStream mFileOutStream = new FileOutputStream(mypath);

                // Set your file path here for sending to server
                FileInputStream fstrm = new FileInputStream(mypath);

                v.draw(canvas); 
                mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); 
                mFileOutStream.flush();
                mFileOutStream.close();
                String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null);
                Log.v("log_tag","url: " + url);
                //In case you want to delete the file
                //boolean deleted = mypath.delete();
                //Log.v("log_tag","deleted: " + mypath.toString() + deleted);
                //If you want to convert the image to string use base64 converter



                // Set your server page url (and the file title/description)
                HttpFileUpload hfu = new HttpFileUpload("http://172.18.1.253:4004/Code/WebSite_SuiviColis/RecSigne.aspx", current,"");

                hfu.Send_Now(fstrm);

            }
            catch(Exception e) 
            { 
                Log.v("log_tag", e.toString());  
            } 
        }

和我的HttpFileUpload我只是从link

得到它

我在这一行得到错误:

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

错误Mesage networkOnMainThreadException

我谷歌它,我发现我必须使它asynxtask或新线程。但我不知道该怎么做。

EDITED

我改变了我的代码:

  Log.v("log_tag","path: " + mypath);
                // Set your file path here for sending to server
                final FileInputStream fstrm = new FileInputStream(mypath);

                // Set your server page url (and the file title/description)
                final HttpFileUpload hfu = new HttpFileUpload("http://172.18.1.253:4004/Code/WebSite_SuiviColis/RecSigne.aspx", current,"");

                new Thread(new Runnable() {
                    public void run() {
                         runOnUiThread(new Runnable() {
                                public void run() {

                                }
                            });                     

                         hfu.Send_Now(fstrm);

                    }
                  }).start(); 

但是现在我得到了服务器retrun 500,java.io的新错误。文件未找到例外。

我把日志放在这里:Log.v("log_tag","path: " + mypath);我已经验证了文件存在。 为什么我找不到文件?

08-21 16:40:15.195: V/log_tag(27436): url: content://media/external/images/media/98
08-21 16:40:15.195: V/log_tag(27436): path: /storage/sdcard0/GetSignature/tty_140056_1026_26754_1_1.png
08-21 16:40:15.203: E/fSnd(27436): Starting Http File Sending to URL
08-21 16:40:15.234: E/fSnd(27436): Headers are written
08-21 16:40:15.242: I/Choreographer(27436): Skipped 39 frames!  The application may be doing too much work on its main thread.
08-21 16:40:15.265: E/fSnd(27436): File Sent, Response: 500
08-21 16:40:15.273: E/fSnd(27436): IO error: http://172.18.1.253:4004/Code/WebSite_SuiviColis/RecSigne.aspx
08-21 16:40:15.273: E/fSnd(27436): java.io.FileNotFoundException: http://172.18.1.253:4004/Code/WebSite_SuiviColis/RecSigne.aspx

2 个答案:

答案 0 :(得分:0)

使用异步任务的示例代码

public class MainActivity extends Activity
{
    MyAsyncTask myTask = new MyAsyncTask();
    String[] params = new String[5];
    myTask.execute(params);
    //Your activity code
}

private class MyAsyncTask extends AsyncTask<String, Integer, Void>
    {

        String path = " ";
        String fileName = " ";
        String providerName = " ";
        long fileSize;
        boolean downloadStatus;
        File downloadedFile;
        int lastPercentage = 0;
        boolean isLoggedIn = true;
        boolean dirChanged = true;
        boolean isCanceled = false;
        boolean failed = false;
        FTPFile[] files;

        @Override
        protected Void doInBackground(String... params) 
        {
            try
            {
                //Upload File

            }

            catch(Exception ex)
            {

            }
            return null;
        }



        @Override
        protected void onCancelled() 
        {



            super.onCancelled();
        }

        @SuppressWarnings({ "static-access" })
        @Override
        protected void onPostExecute(Void result) 
        {

            //Make changes to UI from here
            super.onPostExecute(result);

        }

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            //Do opertaions
        }

        @Override
        protected void onProgressUpdate(Integer... values) 
        {
            //Update if you have a progress bar
        }

    }



}

答案 1 :(得分:0)

得到它,aspx端的错误路径

 string FilePath = Server.MapPath("./Signature/" + vTitle);