Vote count:
0
After almost two weeks... I give up!!!
I achieve upload an image from gallery and camera...
startActivityForResult... ok!
EXTERNAL_CONTENT_URI... ok!
ACTION_IMAGE_CAPTURE... ok!
onActivityResult... ok!
Activity.RESULT_OK ('cause I'm on Fragment)... ok!
getActivity().getContentResolver().query()... ok!
BitmapFactory.Options, opts.inSampleSize, .decodeFile... ok!
but I can't reduce the size of image to 900px, before upload to server using...
- FileInputStream(sourceFile);
- HttpURLConnection
- DataOutputStream( getOutputStream)
- dos.writeBytes(form... name... file name...)
- dos.write(buffer, 0, bufferSize)
I can't understand...
- How use "createScaledBitmap" in this case.
- How can I use "writeBytes(...filename=?)" if when I create a new bitmap, it doesn't have a path (at least I think so).
- If I have an original image on disk, what it's the path of result of "createScaledBitmap"?
- How buffer work (Step by Step would be great), and why in others examples on stackoverflow don't use it?
I have read many references including:
http://ift.tt/IVCkbR
But I already used "options.inSampleSize" for make a Preview on my Fragment, and I think I need (in my case) "createScaledBitmap" for achieve my 900x900px image for upload.
If there is another way to upload images with resize include... let me know!
(Any links would be helpful)
I know... I should use AsyncTask... I'm working on that! ;)
Please consider not talk so technical because I have the worst combination for learning Android: newbie and speak spanish! xD
public int uploadFile(String sourceFileUri) {
final String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
...
} else {
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
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("uploaded_file", fileName);//This is just for info?
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""+fileName+"\"" + lineEnd);//How put in here a resized bitmap?
dos.writeBytes(lineEnd);
//Here I'm lost!
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//I think...
//this is a way to transfer the file in little pieces to server, right?, wrong?
//If anybody can explain this, step by step... THANKS!!!
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "Respuesta HTTP es: " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
...
}
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
...
} catch (Exception e) {
...
}
dialog.dismiss();
return serverResponseCode;
} // End else block
}//End uploadFile()
Resizing image before upload (my upload take/pick is ok)
Aucun commentaire:
Enregistrer un commentaire