Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

DaveJ

(5,023 posts)
Mon Dec 19, 2011, 10:32 AM Dec 2011

Android code instructions on uploading picture



I really just need a tiny bit more help with my Android app and might be able to proceed from there...

These instructions are driving me a little crazy. I like this site linked below, but it's sort of ridiculous how they provide the minimum information on every question, basically once they answer and gets their points, they disappear. No conversation is even allowed on that site.

Anyway, this post has instructions on uploading a picture http://stackoverflow.com/questions/2935946/sending-images-using-http-post . I inserted all this code into my app, but it's odd how they don't mention 1) where you actually type in the destination server, or 2) how you properly authenticate to the server, or 3) how the picture is attached during this process (but maybe it's already in the code, not sure).

They are like "bla here's some code". Anyway, I'm just joking around, sorry for the attitude.

Any help with indicating a destination server would sure be appreciated.
7 replies = new reply since forum marked as read
Highlight: NoneDon't highlight anything 5 newestHighlight 5 most recent replies
Android code instructions on uploading picture (Original Post) DaveJ Dec 2011 OP
I'm fairly new to Android development but... SirRevolutionary Dec 2011 #1
I'll give it a try later tongiht DaveJ Dec 2011 #2
My guess is it's probably very similar for .NET authentication SirRevolutionary Dec 2011 #3
Still having trouble, with server side DaveJ Dec 2011 #4
I got the upload to work with this code SirRevolutionary Dec 2011 #5
Very cool accomplishment, but what is the simple .php file you used? DaveJ Dec 2011 #6
Ha! The programmer who can't remove smileys from the code SirRevolutionary Dec 2011 #7

SirRevolutionary

(579 posts)
1. I'm fairly new to Android development but...
Mon Dec 19, 2011, 11:10 AM
Dec 2011

from reading the code snippet, I would think you set the destination to the server via the HttpPost httpPost = new HttpPost(url);

The method's signature would take the full URL path to post the data to: public void post(String url, ...

So that would be something like http://myserver.com/myUploadFile.php or whatever. For authenticating, I'd imagine you do that before you get anywhere near attempting to upload data. It could be done the same way as this code snippet. Have the user enter a name and password, send it to the server and authenticate before trying to upload anything.

Inside the conditional if/else statement in that code, it looks like it's going to send either an actual file (the image) or just a string to the server.

There's probably more astute Android developers on DU who could chime in and elaborate though.

DaveJ

(5,023 posts)
2. I'll give it a try later tongiht
Mon Dec 19, 2011, 04:36 PM
Dec 2011

I can't work on this at work since its a personal project, so it'll be this evening sometime.

I overlooked the first line in the code that has "String url" in it. I should have seen that.

I'm still unclear about some things, Post authentication might work differently on an IIS server vs an Apache server. Just a guess. I usually just code in .Net without thinking about how posts work. But I should be able to figure it out from here. I'll let you know how it goes. Are you currently doing Android development? Thanks.

SirRevolutionary

(579 posts)
3. My guess is it's probably very similar for .NET authentication
Mon Dec 19, 2011, 06:06 PM
Dec 2011

I'd guess it's just a matter of hitting the server through HTTPS and just sending the name/password to log in. I actually came from a C# .NET background and got more into Java over the years. For Android specifically, I'm pretty much a noobie with limited knowledge of the mobile API's. I do have a few apps that I built with other tech that I'm trying to convert to native Android, so I'm in the same boat trying to pick up knowledge as I go. Best way to do it is just start building. Definitely give a shout if that works, there may be better Java packages for authentication by now.

DaveJ

(5,023 posts)
4. Still having trouble, with server side
Tue Dec 20, 2011, 10:34 AM
Dec 2011

This is turning out to be a very undocumented procedure. Or maybe I'm just not seeing it. I created a page called GetMobileImage.aspx and placed the method below into it. But I can't figure out what to do from there. I'd expect something would go into the Page_Load method, to receive the transfer on the server side, but so far, I have not found any clear instructions online, just bits and pieces.

I'll keep trying, and definitely get back when I figure this out. It's probably so straightforward that nobody else is bothering to ask.

I'm thinking about uploading to some service like Photobucket, but the images would be better off on my own server.

Thanks.


private byte[] GetFileByteArray(string filename)
{
FileStream oFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);

// Create a byte array of file size.
byte[] FileByteArrayData = new byte[oFileStream.Length];

//Read file in bytes from stream into the byte array
oFileStream.Read(FileByteArrayData, 0, System.Convert.ToInt32(oFileStream.Length));

//Close the File Stream
oFileStream.Close();

return FileByteArrayData; //return the byte data
}

SirRevolutionary

(579 posts)
5. I got the upload to work with this code
Tue Dec 20, 2011, 12:04 PM
Dec 2011

Found it here http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

I used a simple PHP file on the server and just barely tweaked the sample that blog post provided. Also, he mentions you need the HttpClient 4.1-Beta1 jar (beta at the time), but you'll need to add the httpclient and httpmime jars to your Android project.

package com.test.testupload;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

public class TestUploadActivity extends Activity {
Bitmap bm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
bm = BitmapFactory.decodeFile("/sdcard/DCIM/100MEDIA/FOO.jpg&quot ;
executeMultipartPost();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}

public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://myserver.com/testUpload.php&quot ;
ByteArrayBody bab = new ByteArrayBody(data, "FOO.jpg&quot ;

MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("ufile", bab); // this is the PHP post var
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8&quot );
String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s); // PHP echoes ok or fail
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
}

This was a fun exercise I don't know how to remove the emoticons from the code in a DU post, pretty ironic for a developer, no?

DaveJ

(5,023 posts)
6. Very cool accomplishment, but what is the simple .php file you used?
Tue Dec 20, 2011, 09:04 PM
Dec 2011

It's astounding to me, that you so quickly created this upload utility for Android. Maybe I should throw in the towel and just sell caramel popcorn instead.

Problem is, I'm really still wondering what is in the server side code? Did you show the .php file somewhere and I missed it? And what is the C# version of the file?

I must be missing something totally obviously.

Oh, also if you could figure out how to prevent those smiley's from appearing, many people would be amazed.

SirRevolutionary

(579 posts)
7. Ha! The programmer who can't remove smileys from the code
Tue Dec 20, 2011, 09:50 PM
Dec 2011

That one baffled me, so I gave up

I'm not new to Java, but fairly new to Android and this is a different sample than that StackFlow one, this one was a lot easier. I mostly use PHP and Java on Apache, so I'd be really rusty on C# and IIS. Plus I'm set up on Mac now so I don't have the Visual Studio IDE set up anymore, but there's tons of simple samples out there. Things are definitely different for IIS, permissions to upload files to folders are probably handled much differently.

If you want to do it the right way, I'm sure you'd want to create a simple web service if it's just for your Android app to upload images http://www.c-sharpcorner.com/UploadFile/scottlysle/UploadwithCSharpWS05032007121259PM/UploadwithCSharpWS.aspx I can't speak for that example since I'm so rusty with .NET and IIS. But that's the route I'd take.

Just remember the post variable name in the Android code is called 'ufile'. So whatever .NET expects, you'll probably need to match the names.

If you want, send me a PM with an email address and I'll email you the plain text for the Activity Java class for Android and the PHP upload script so there's no smiley emoticons. There's no security or anything for the PHP script, it's just quick and dirty, takes a file and saves it somewhere, nothing fancy.

Latest Discussions»Retired Forums»Website, DB, & Software Developers»Android code instructions...