3
在我的应用程序中,我试图从设备上捕捉图像,并且想将其上传到服务器。 我下面这被张贴here在Android相机中上传拍摄图像的问题
继答案是我启动相机
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startCamera();
}
});
}
public void startCamera()
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
但之后,我捕捉,当我试图把它上传被撞坏的代码。在logcat中它显示的错误 在OnActivity结果并在doFileUpload
以下是我的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(resultCode)
{
case 0:
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
doFileUpload(MediaStore.EXTRA_OUTPUT);
}
private void doFileUpload(String extraOutput)
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inStream = null;
String urlServer = "http:XXXXXXXXXXXXXXXXXXXXXX/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
FileInputStream fileInputStream = new FileInputStream(new File(extraOutput));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new
DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + extraOutput +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens +lineEnd);
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
请帮我修我的错误...... PLZ
你必须更具体。 LogCat显示什么样的错误? – 2011-05-27 09:02:34
除此之外:你不应该在UI线程中做网络操作,这是一个很大的不是。 – 2011-05-27 09:03:27
@Siva K:你能给我完整的代码吗? – 2012-11-27 06:15:44