I'm starting a new thread, in response to a request over here:
http://bit.ly/E1Qqm
This is some sample code for uploading an image to the web, as part of
a multipart message. It assumes that the photo is stored on the SD
card as "photo.jpg". You'll need to download and add a couple of jar
files to the project's built path - commons-httpclient.jar and
httpcomponents-client-4.0-alpha4.
It is working on my device. However (big caveats here) - it's much
slower than I would like, and much slower than other applications I've
installed that upload photos. It also feels hacky - e.g. the way the
background thread communicates problems with the upload to the main
thread using variables. I'm sure there must be a neater way to do it.
If you figure out a way to improve the code, please post suggestions
here!
cheers,
Anna
-------------------------------------
private static final int LOCATION_NOT_FOUND = 1;
private static final int UPLOAD_ERROR = 2;
final Runnable mUpdateResults = new Runnable() {
public void run() {
pd.dismiss();
updateResultsInUi();
}
};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.home);
// do some stuff and then, when you want to upload a
photo....
uploadToWeb();
}
//
**********************************************************************
// uploadToWeb: uploads details, handled via a background thread
//
**********************************************************************
private void uploadToWeb() {
Log.d(LOG_TAG, "uploadToWeb");
pd = ProgressDialog
.show(
this,
"Uploading, please wait...",
"Uploading. This can take several seconds, depending on your
connection speed. Please be patient!",
true, false);
Thread t = new Thread() {
public void run() {
doUploadinBackground();
mHandler.post(mUpdateResults);
}
};
t.start();
}
private void updateResultsInUi() {
if (globalStatus == UPLOAD_ERROR) {
showDialog(UPLOAD_ERROR);
} else if (globalStatus == LOCATION_NOT_FOUND) {
showDialog(LOCATION_NOT_FOUND);
} else if (globalStatus == PHOTO_NOT_FOUND) {
showDialog(PHOTO_NOT_FOUND);
} else {
// Success! - Proceed to the success activity!
Intent i = new Intent(Home.this, Success.class);
startActivity(i);
}
}
//
**********************************************************************
// onCreateDialog: Dialog warnings
//
**********************************************************************
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case UPLOAD_ERROR:
return new AlertDialog.Builder(Home.this)
.setTitle("Upload error")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
})
.setMessage(
"Sorry, there was an error uploading. Please try again later.")
.create();
case LOCATION_NOT_FOUND:
return new AlertDialog.Builder(Home.this)
.setTitle("Location problem")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
})
.setMessage(
"Could not get location! Can you see the sky? Please try again
later.")
.create();
}
return null;
}
Part[] parts = { new StringPart("service", "Android mobile"),
new StringPart("subject", subject),
new StringPart("name", name),
new StringPart("email", email),
new StringPart("lat", latString),
new StringPart("lon", longString), photo };
if (responseString.equals("SUCCESS")) {
// launch the Success page
return true;
} else {
globalStatus = UPLOAD_ERROR;
return false;
}
}
// read the photo file into a byte array...
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)
{
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
Awesome, thanks for sharing the code. :) Does it need a specific version of HttpClient, different than the one that comes with the platform?
And for communicating status and progress back up to a UI thread, AsyncTask in 1.5 would be perfect. Also, what license is this code snippet released under? Thanks again for sharing! :)
On Sun, May 10, 2009 at 5:04 AM, Anna PS <annapowellsm...@googlemail.com> wrote:
> I'm starting a new thread, in response to a request over here: > http://bit.ly/E1Qqm
> This is some sample code for uploading an image to the web, as part of > a multipart message. It assumes that the photo is stored on the SD > card as "photo.jpg". You'll need to download and add a couple of jar > files to the project's built path - commons-httpclient.jar and > httpcomponents-client-4.0-alpha4.
> It is working on my device. However (big caveats here) - it's much > slower than I would like, and much slower than other applications I've > installed that upload photos. It also feels hacky - e.g. the way the > background thread communicates problems with the upload to the main > thread using variables. I'm sure there must be a neater way to do it.
> If you figure out a way to improve the code, please post suggestions > here!
> cheers, > Anna
> -------------------------------------
> private static final int LOCATION_NOT_FOUND = 1; > private static final int UPLOAD_ERROR = 2; > final Runnable mUpdateResults = new Runnable() { > public void run() { > pd.dismiss(); > updateResultsInUi(); > } > };
> public void onCreate(Bundle icicle) { > super.onCreate(icicle); > setContentView(R.layout.home);
> // do some stuff and then, when you want to upload a > photo.... > uploadToWeb(); > }
> // > ********************************************************************** > // uploadToWeb: uploads details, handled via a background thread > // > ********************************************************************** > private void uploadToWeb() { > Log.d(LOG_TAG, "uploadToWeb"); > pd = ProgressDialog > .show( > this, > "Uploading, please wait...", > "Uploading. This can take several seconds, depending on your > connection speed. Please be patient!", > true, false); > Thread t = new Thread() { > public void run() { > doUploadinBackground(); > mHandler.post(mUpdateResults); > } > }; > t.start(); > }
> Part[] parts = { new StringPart("service", "Android mobile"), > new StringPart("subject", subject), > new StringPart("name", name), > new StringPart("email", email), > new StringPart("lat", latString), > new StringPart("lon", longString), photo };
I do something very similar to this but not with images, but rather
with torrent files. I don't have a 'bytes from file' method though,
probably works because torrent files are just text. Anyway, thanks for
sharing.
On May 10, 10:31 am, Jeff Sharkey <jshar...@android.com> wrote:
> Awesome, thanks for sharing the code. :) Does it need a specific
> version of HttpClient, different than the one that comes with the
> platform?
> And for communicating status and progress back up to a UI thread,
> AsyncTask in 1.5 would be perfect. Also, what license is this code
> snippet released under? Thanks again for sharing! :)
> j
> On Sun, May 10, 2009 at 5:04 AM, Anna PS <annapowellsm...@googlemail.com> wrote:
> > I'm starting a new thread, in response to a request over here:
> >http://bit.ly/E1Qqm
> > This is some sample code for uploading an image to the web, as part of
> > a multipart message. It assumes that the photo is stored on the SD
> > card as "photo.jpg". You'll need to download and add a couple of jar
> > files to the project's built path - commons-httpclient.jar and
> > httpcomponents-client-4.0-alpha4.
> > It is working on my device. However (big caveats here) - it's much
> > slower than I would like, and much slower than other applications I've
> > installed that upload photos. It also feels hacky - e.g. the way the
> > background thread communicates problems with the upload to the main
> > thread using variables. I'm sure there must be a neater way to do it.
> > If you figure out a way to improve the code, please post suggestions
> > here!
> > cheers,
> > Anna
> > -------------------------------------
> > private static final int LOCATION_NOT_FOUND = 1;
> > private static final int UPLOAD_ERROR = 2;
> > final Runnable mUpdateResults = new Runnable() {
> > public void run() {
> > pd.dismiss();
> > updateResultsInUi();
> > }
> > };
Thank you very much for sharing this great code!
It helped me a lot!
But I have on problem! I have created a ruby on rails webapp where I
want to upload my image.
On the website I can upload a image without a problem but when I want
to upload the image with Android, the image cannot be uploaded for one
reasen (but I don't know which)!
I make a post request on the server (it passes all the right
paramaters en the uploaded_data from the photo) but it won't save my
photo in the database or upload it on my website..
Do i have to do something special with the website so I can upload an
image from android to my website (server)?
Thank you,
Wouter
On May 11, 4:51 am, "nEx.Software" <justin.shapc...@gmail.com> wrote:
> I do something very similar to this but not with images, but rather
> with torrent files. I don't have a 'bytes from file' method though,
> probably works because torrent files are just text. Anyway, thanks for
> sharing.
> On May 10, 10:31 am, Jeff Sharkey <jshar...@android.com> wrote:
> > Awesome, thanks for sharing the code. :) Does it need a specific
> > version of HttpClient, different than the one that comes with the
> > platform?
> > And for communicating status and progress back up to a UI thread,
> > AsyncTask in 1.5 would be perfect. Also, what license is this code
> > snippet released under? Thanks again for sharing! :)
> > j
> > On Sun, May 10, 2009 at 5:04 AM, Anna PS <annapowellsm...@googlemail.com> wrote:
> > > I'm starting a new thread, in response to a request over here:
> > >http://bit.ly/E1Qqm
> > > This is some sample code for uploading an image to the web, as part of
> > > a multipart message. It assumes that the photo is stored on the SD
> > > card as "photo.jpg". You'll need to download and add a couple of jar
> > > files to the project's built path - commons-httpclient.jar and
> > > httpcomponents-client-4.0-alpha4.
> > > It is working on my device. However (big caveats here) - it's much
> > > slower than I would like, and much slower than other applications I've
> > > installed that upload photos. It also feels hacky - e.g. the way the
> > > background thread communicates problems with the upload to the main
> > > thread using variables. I'm sure there must be a neater way to do it.
> > > If you figure out a way to improve the code, please post suggestions
> > > here!
> > > cheers,
> > > Anna
> > > -------------------------------------
> > > private static final int LOCATION_NOT_FOUND = 1;
> > > private static final int UPLOAD_ERROR = 2;
> > > final Runnable mUpdateResults = new Runnable() {
> > > public void run() {
> > > pd.dismiss();
> > > updateResultsInUi();
> > > }
> > > };
Thank you very much for sharing this great code!
It helped me a lot!
But I have on problem! I have created a ruby on rails webapp where I
want to upload my image.
On the website I can upload a image without a problem but when I want
to upload the image with Android, the image cannot be uploaded for one
reasen (but I don't know which)!
I make a post request on the server (it passes all the right
paramaters en the uploaded_data from the photo) but it won't save my
photo in the database or upload it on my website..
Do i have to do something special with the website so I can upload an
image from android to my website (server)?