[英]How can i send image to server ASP.NET from Android
我想使用按鈕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我只是通過此鏈接獲取它
我在這條線上有錯誤:
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
錯誤消息networkOnMainThreadException
我用谷歌搜索,發現我必須使它成為asynxtask或新線程。 但我不知道該怎么做。
已編輯
我已經更改了我的代碼,如下所示:
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();
但是現在我收到新錯誤,服務器重新運行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
使用異步任務的示例代碼
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
}
}
}
知道了,aspx一側的路徑錯誤
string FilePath = Server.MapPath("./Signature/" + vTitle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.