I am brand new to Android development, and have run into an issue with my first major app that I would very much appreciate help on. The goal of this app is to allow the user to select an image stored on the phone, and upload that image to a server via a http post method, taking the JPEG binary data as a parameter. The catch is that I need to preserve the EXIF data on this as well. I currently have pushed an image onto my emulator's sdcard, and used a basic app to confirm that it is there with the EXIF data.
Here is what I've got so far:
To provide the user with a choice of images, and as a result obtain the image Uri, I use:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);
In my activity result code, I obtain my Uri with:
Uri selectedImage = data.getData();
I use this to get a bitmap of the image and display it to the user as a preview.
Here is where I get stuck, as I'm not sue how best to upload it. I have looked a lot at this example in hopes of doing something similar, but he seems to be just compressing a bitmap and sending it, which would probably destroy the EXIF data I need, plus I can't even seem to get that to work.
This is the code I'm trying right now, which given my low experience with such things may be entirely wrong:
Uri selectedImage = data.getData();
InputStream imageInputStream=this.getContentResolver().openInputStream(selectedImage);
String str=imageInputStream.toString();
byte[] imageBits=str.getBytes();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("where I am sending it");
ByteArrayBody toUpload = new ByteArrayBody(imageBits, "androidpic.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("userImage", toUpload);
reqEntity.addPart("FileName", new StringBody("android pic"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
This of course does not seem to work.
Does anyone have experience uploading jpegs from the phone like this who could give me some pointers? Any help would be greatly appreciated.
Related
I'm new to android programming. I'm looking for a simple way to send pictures to Picasa, I looked at a lot of projects on it. I'm just looking to send a JPEG or PNG button I click, sends and displays a message that it is OK.
I know that is required a Google API and client authentication, but a lot of people show the same Intention sent.
Please help (sorry for the english: P)
I found this:
http://code.google.com/p/google-api-java-client/source/browse?repo=samples#hg/picasa-android-sample
Someone knows how to use it? But from the basics, I'm lost.
The only existing code in online for uploading photos to Picasa is this one..
Picasa Photo Uploader
Try with this one whether it can meet your requirements.If it does,then engage it with a button click event and display message on notification.finished() event to ensure that the file has been uploaded.
Quite an old post, but just for future references, I was successful in directly using http post to upload my image to Picasa. Their own Java API keeps returning errors.
I've written about this method in detail here:
File image = new File("/path/to/image.jpg");
byte[] imageContent = null;
try {
imageContent = Files.toByteArray(image);
} catch (Exception e) {
// do something
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://picasaweb.google.com/data/feed/api/user/default/albumid/default");
httpPost.addHeader("Authorization", "Bearer " + mAccessToken);
httpPost.addHeader("Content-Type", "image/jpeg");
httpPost.setEntity(new ByteArrayEntity(imageContent));
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// log the response
logd(EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e){
// do something
}
This method uses Apache's HttpClient. If your Android version does not support it, you can still include this line in your Gradle file to compile it:
compile 'cz.msebera.android:httpclient:4.4.1.1'
I have this gallery that loads images from urls, i'm first retrieving an InputStream like this:
HttpParams httpparameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparameters, 3000);
HttpConnectionParams.setSoTimeout(httpparameters, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpparameters);
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
And then, I read the stream with Bitmap bitmap = BitmapFactory.decodeStream(is, null, opts2);
The problem here is that sometimes i get the out of memory error. I fixed by modifying the inSampleSize, but I dont think its a good workdaround since the quality of the images are not good. Is there any way to load a list of images without getting the outofmemory and with the same quality as in the server? For example, in facebook app, when you enter to the gallery, it do a lazy load, you can see when you are in the grid layout, and when you open the image in full mode, you can scroll and see how it download all the images, and no out of memory issue, or lag while retrieving.
Or if you know of any android library that do this so i dont need to reinvent the wheel
Thank you
There was a nice talk on the Google IO 2012 about IO Gallery application you may easily find on YouTube, with the easy-to-download source code you may get all your answers from: http://code.google.com/p/iogallery/
So ultimately I'm trying to upload images that I want Google to OCR. Then I want to be able to get the results of the OCR back to my Android app. I have my images uploading properly. I can loop through all the files in my google drive and I see that there are export links available, one of which is "text/plain". If I use one of these urls in a browser, it downloads the text. Is this the way I should be trying to access it?
I've tried to use the url I get from calling getExportLinks method on the file returned by the insert method
File file = drive.files().insert(body, mediaContent).setOcr(true).execute();
String imageAsTextUrl = getExportLinks.get("text/plain")
I end up getting HTML back that appears to be the Google Drive home page. To get the exported url document, I used google drive instance so it should have properly authenticated like the insert method I would think.
DriveRequest request = new DriveRequest(drive, HttpMethod.GET, imageAsTextUrl, null);
Has anyone tried to do this before? What am I doing wrong?
Well I answered my own question yet again, sort of. Basically since this seems to be a web url and not an API call I can make, then it's not responding with a 401 if it's unauthenticated. So basically the response I was getting is the HTML for the login page. Apparently using DriveRequest does not automatically handle authentication like I thought it would. So I have it working by adding authentication manually to an HttpClient GET call.
But is there a way to do what I'm trying to do with the actual API? So I can deal with response codes?
Here's what I did to download the text/plain representation of the file. Here's a caveat: given that the image I was uploading was taken on a cell phone camera using the default camera app, the default dpi and/or jpeg compression caused the OCR to not work very well. Anyway, here's the code I used. Just basic HttpClient stuff
String imageAsTextUrl = file.getExportLinks().get("text/plain");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(imageAsTextUrl);
get.setHeader("Authorization", "Bearer " + token);
HttpResponse response = client.execute(get);
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
}
finally {
if (in != null) {
in.close();
}
}
// Send data to new Intent to display:
Intent intent = new Intent(UploadImageService.this, VerifyTextActivity.class);
intent.putExtra("ocrText", sb.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am creating a sign up form where i need to send the following data to the PHP server to create the user account
First Name
Last Name
Email
PAssword
Image
i am sending the first four via JSON. now i am wondering how to include the image and send it to the server.
for this use the multipartentity concept.
for reference see the below code
MultipartEntity req=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "icon.png");
req.addPart("image", bab);
httppost.setEntity(req);
in that req.addPart("image", bab); "image" is the xml code.u sholud collect it .
you can transfer byteStream of image by HttpConnection .
i followed this link for the same .
you should go for Base64 encoding to send Image to sever.
see this link..Binary Data in JSON String. Something better than Base64
First you need to decide what kind of image you want to send. Do you want to choose an image from sd-card or take a photo with camera?
Here is very good tutorial how to do it, which even includes explanation how to implement croping of the image.
Next step, you will need to upload this file. You can get good information about that from here.
This is how my programme works:
1) Display a picture from the server
2) User change the picture and upload it to the server
3) Display the picture by re-downloading from the server
This is how I get the picture from the server:
String src = "http://www.getyourpicture.com/mypicture.jpg"
HttpGet httpRequest = new HttpGet(URI.create(src) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap dp = BitmapFactory.decodeStream(instream);
//display dp from here...
The problem here is, whenever I "re-download" the image, it still shows the old picture.
To confirm that I've uploaded the picture, I've checked the folder containing the picture on the server and even visited the link on a browser. Both approaches show that the picture is indeed been uploaded. So I've narrowed down to the possibility that Android might have a http caching manager that is not "refreshing" the image link.
So, if the answer to my question is "yes", how can I force the application to not use the cache?
If the answer is "no", what is the thing that I've overlooked?
I'm not sure about the undercovers working and the defaults of HTTP request caching on Android, but if it is decent, then it should in theory suffice to add a query string with a timestamp to the request URL to trigger a brand new and fullworthy HTTP request.
String src = "http://www.getyourpicture.com/mypicture.jpg?" + System.currentTimeMillis();