Im trying to upload image to server, but somehow the code stopped at FileInputStream line. not sure why and i don't know how to debug or check it. here my source code:
public class CreateSetcardStep1Activity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
String filename = appHelper.dateToString(new Date(), "yyyyMMdd-hhmmss");
destination = new File(Environment.getExternalStorageDirectory(), filename + ".jpg");
buttonTakePhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
}
});
buttonSubmitPhoto.setVisibility(View.GONE);
buttonSubmitPhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog = ProgressDialog.show(CreateSetcardStep1Activity.this, "", "Uploading file...", true);
new Thread(new Runnable() {
public void run() {
uploadFile(imagePath);
}
}).start();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
try {
buttonSubmitPhoto.setVisibility(View.VISIBLE);
Log.e(LOG, "breakpoint 1");
setcardPic.setVisibility(View.VISIBLE); Log.e(LOG, "breakpoint 2 destination: "+destination);
FileInputStream in = new FileInputStream(destination); Log.e(LOG, "breakpoint 3");
BitmapFactory.Options options = new BitmapFactory.Options(); Log.e(LOG, "breakpoint 4");
options.inSampleSize = 10; Log.e(LOG, "breakpoint 5");
imagePath = destination.getAbsolutePath(); Log.e(LOG, "breakpoint 6 imagePath: "+imagePath);
Log.e(LOG, "PATH === " + imagePath);
//tvPath.setText(imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
setcardPic.setImageBitmap(bmp);
showUploadButton = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
showUploadButton = false;
Toast.makeText(getApplicationContext(),
R.string.request_cancelled,
Toast.LENGTH_LONG).show();
}
}
public int uploadFile(String sourceFileUri) {
String fileName = sourceFileUri;
showUploadButton = false; //revert upload button to hidden
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
dialog.dismiss();
Log.e(LOG, "Source File not exist :" +imagePath);
return 0;
}
else
{
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Filedata", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename="+ fileName + "" + lineEnd);
dos.writeBytes(lineEnd);
// 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);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i(LOG, "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CreateSetcardStep1Activity.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CreateSetcardStep1Activity.this, "MalformedURLException",
Toast.LENGTH_SHORT).show();
}
});
Log.e(LOG, "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CreateSetcardStep1Activity.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e(LOG, "Exception : "
+ e.getMessage(), e);
}
dialog.dismiss();
return serverResponseCode;
}
}
}
Is there any way to check where the issue is? the logcat stopped at breakpoint 2: breakpoint 2 destination: /storage/emulated/0/20171215-051851.jpg so the file is there if i check the explorer