在我的应用程序中我正在尝试捕获图像并且我想将其发送到网址,但问题是当活动开始并且相机被打开时,在我捕获图像之前上传到服务器部分获取开始,因此图像没有上传。
以下是我的代码,请帮助我解决问题...
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);
}
}
答案 0 :(得分:1)
您没有从
获取结果的函数startActivityForResult(intent, TAKE_RECEIPT);
您需要实施此方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {