Hi everyone...
I wouldn't post about this again if I really wasn't frustrated about
it.. I've been going through this for a whole week and still I'm not
able to do an image upload... My code doesn't report any errors.. but
not even the intent is working.. please take a minute to go through
it.. it's kinda simple.. like I want it to be..
This is my addPic.class on which I have a ImageView, EditText, a
Button to browse the device and a button to add the picture...
This is the code for the browse button:
deviceBrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i,
ACTIVITY_SELECT_IMAGE);
}
});
which calls :
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode,
imageReturnedIntent);
switch(requestCode) {
case ACTIVITY_SELECT_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn =
{MediaStore.Images.Media.DATA};
Cursor cursor =
getContentResolver().query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex =
cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
selectedDeviceImage =
BitmapFactory.decodeFile(filePath);
picPreview.setImageBitmap(selectedDeviceImage);
label.setText(filePath);
exsistingFileName = filePath;
}
}
}
This last piece of code ..put's the path of the file in the label and
the bitmap on the Imageview so I can have a preview..
So here is my problematic piece of code: the add button
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doFileUpload();
// Go to camera mode
Intent addIntent = new
Intent(v.getContext(), editPic.class);
Bundle w = new Bundle();
String activityName = "addPic";
w.putString("activity", activityName);
w.putParcelable("bitmap", selectedDeviceImage);
//a.putString("id", picId);
addIntent.putExtras(w);
startActivity(addIntent);
finish();
I'll show you the doFileUpload() in a minute... I just wanted to
mention that the intent is not working God knows why.. it doesn't go
to the class I specified...
private void doFileUpload(){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
exsistingFileName = filePath;
// Is this the place are you doing something wrong.
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://www.pedroteixeira.org/thennnow/
UT9.php";
try
{
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new
File(exsistingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
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);
}
}
On the server side.. the php is working since I've tested it with HTML
form.. it's just this piece of code which doesnt work .. I have no
ideia.. can someone please help me? :(
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en