Uploading to picasa from android app - android

I'm developing an app for which I need to be able to upload pics to picasa open album.
Ive gone through many threads,forums... tried a couple of methods using http post but none seems to work.
Has anyone did it before? If so can you share a sample code.I just need to do the basic upload and dload of pics from picasa.

The following question looks to cover some of this.
Picasa access in android: PicasaUploadActivity
This thread also has information. http://www.mail-archive.com/android-developers#googlegroups.com/msg43707.html
It looks straight forward to fire off the intent to use the standard picasa uploader. I will try putting this in my app later today as I want this function.
Doing it yourself looks to be possible but clearly more complex documentation looks to be http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html
OK I have got it working with the following code in my app. This brings up the picasa uploader.
Intent temp = new Intent(Intent.ACTION_SEND);
temp.setType("image/png");
temp.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
temp.putExtra(Intent.EXTRA_STREAM, fileUri);
temp.setComponent(new ComponentName(
"com.google.android.apps.uploader",
"com.google.android.apps.uploader.clients.picasa.PicasaSettingsActivity"));
try {
startActivity(temp);
} catch (android.content.ActivityNotFoundException ex) {
Log.v(TAG, "Picasa failed");
}
In practice I am going to take the set component bit out which lets the use choose where and how to send which is what I want.

The above answer was for Picasa API v2, which is now deprecated. I was not able to successfully use the Java API for Picasa API v3, but I figured out a way to upload images to Picasa using http post. I've written about this method 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'

Related

Upload photo to picasa from android?

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'

Google Drive SDK - Uploading Image, OCR, Download Result

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);

Using Android's System Cache

I would like to use Android's system cache when downloading images as per these previous instructions: android system cache. I was able to get the following code working but the log statements are telling me that the images are never being read from the cache.
try {
//url = new URL("http://some.url.com/retrieve_image.php?user=" + username);
URL url = new URL("http://some.url.com/prof_pics/b4fe7bdfa174ff372c9f26ce6f78f19c.png");
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
Log.i("CHAT", "this is a bitmap");
current_image.setImageBitmap((Bitmap) response);
}
else {
Log.i("CHAT", "this is not a bitmap");
Log.i("CHAT", response.toString());
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
current_image.setImageBitmap(BitmapFactory.decodeStream(bis));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have tried two different types of requests, one is to go through a PHP script that returns the image and another that is directly accessing the image file. I refresh the same image multiple times in a row and it never seems to get cached. For the direct image access, I get:
05-31 23:45:12.177 I/CHAT ( 2995): this is not a bitmap
05-31 23:45:12.177 I/CHAT ( 2995): org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream#40c1c660`
For the indirect image access, I consistently get:
05-31 23:45:14.550 I/CHAT ( 2995): this is not a bitmap
05-31 23:45:14.550 I/CHAT ( 2995): org.apache.harmony.luni.internal.net.www.protocol.http.ChunkedInputStream#40c02448
I found a better way to do it. If anyone else is having trouble after following the link android system cache, use this Google developer's blog post instead. The source code in that blog post is designed for a ListView but I am using it for all image retrievals. It downloads the image in an AsyncTask, puts a temporary image while downloading, and has an image cache. This last part is listed as a "Future Item" in the blog post, but if you download the source code, the cache is implemented. I had to modify the code slightly because the AndroidHttpClient isn't supported in 2.1. I switched it to a URL connection. So far, this looks to be a great image downloader class. Let's just hope it doesn't impact our already struggling memory management issues.

Dropbox Sharing file URL

I am developing an application for Android and which uses Dropbox for organizing the files. I am exploring the Dropbox API but its description and help is limited, as there is no documentation for the Dropbox API.
I still would like to manage the files to some functionality, for example placing a file and getting a file from Dropbox. Now the problem is when I put some files in Dropbox public folder and I need a URL to share to my contacts in the application. But in the API I could not find any function that returns the web URL of the file to share (Just like in the Deskotop interface of Dropbox, a user can get a Shared URL to send to friends).
Could someone help me figure out how to share that file with contacts in the Application?
Or any other way to share a file using Dropbox Android API?
According to changes made on DropBox metioned here: https://www.dropbox.com/help/16/en
There would be no more Public folders, instead access to files can be done via Share Link.
If you use Android DropBox Core Api then shared link can be retrieved this way:
// Get the metadata for a directory
Entry dirent = mApi.metadata(mPath, 1000, null, true, null);
for (Entry ent : dirent.contents) {
String shareAddress = null;
if (!ent.isDir) {
DropboxLink shareLink = mApi.share(ent.path);
shareAddress = getShareURL(shareLink.url).replaceFirst("https://www", "https://dl");
Log.d(TAG, "dropbox share link " + shareAddress);
}
}
UPDATE: 2014/07/20 by Dheeraj Bhaskar
Use the following helper function alongwith the above function.
Since DropBox started to send shortened links it is little bit more problematic to get proper link.
For now, I am using this method :
We simply load the URL, follow the redirects and get the new URL.
String getShareURL(String strURL) {
URLConnection conn = null;
String redirectedUrl = null;
try {
URL inputURL = new URL(strURL);
conn = inputURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
System.out.println("Redirected URL: " + conn.getURL());
redirectedUrl = conn.getURL().toString();
is.close();
} catch (MalformedURLException e) {
Log.d(TAG, "Please input a valid URL");
} catch (IOException ioe) {
Log.d(TAG, "Can not connect to the URL");
}
return redirectedUrl;
}
Note: All of this should be done of course in AsyncTask or Thread. This will produce proper links ready to download
Update 2014/07/25: Change in dropbox share URLs
A heads-up on the kind of URLs to expect
From the Dropbox team:
We wanted to give you a heads up about an upcoming change to the URL
structure of Dropbox shared links. While not part of the API, the
change could affect apps that manipulate the URLs returned from the
/shares endpoint or the "preview" link type returned by the Chooser
Drop-in.
Links returned will now have a ?dl=0 appended to them.
E.g., instead of
https://www.dropbox.com/s/99eqbiuiepa8y7n/Fluffbeast.docx, you'll
receive URLs
like this link
https://www.dropbox.com/s/99eqbiuiepa8y7n/Fluffbeast.docx?dl=0.
A useful thread in the Dropbox forums:
http://forums.dropbox.com/topic.php?id=37700&replies=7#post-326432
IF The public link for a file is always
dl.dropbox.com/u/<your users uid>/<path under /Public>/filename
then we can just use the API to get and build the public URL in the code.
Perhaps this may also help: Upload a file to Dropbox and copy public address. This script upload a file to your /Public directory and use your accound
UID to build it's public URL. Then, it echoes the URL to the console.
https://github.com/sylvainfilteau/dropbox-api-command/commit/6aa817c79220c5de4ff5339cd01ea8b528bcac36
I am not there yet in my Dropbox interface implementation, but this is one of the functions I need to develop. More in one or two days I hope.
I believe the url is as follows:
http://dl.dropbox.com/u/YOUR_DROPBOX_ID/YOUR_FILE_NAME

Find nearest resturant, coffee shop, and grocery store using google maps in android

I am working in android sdk and using google maps api. I wanted to know if there is any way to find the nearest resturant or coffee shop or a grocery store from current location of the user.
There are many apps available for this purpose but i want to code my own application for learning purpose.
This may help, dont know if it works for android..
http://code.google.com/apis/maps/documentation/places/
I've had the same issue making a Navigation program for Android.
I ended up using Yahoo! Local Search web service, it worked pretty well.
You should understand how web services work, but basically you make an HTTP GET request with the parameters, such as Location, Query (restaurant, coffee, etc) and other parameters, and you get a response as XML or JSON (your choice).
What you do with the results is up to you
Additions:
Yahoo Local Search results are default to XML.
This is an example of how to make the request in Android:
public void doWebRequest()
{
try {
HttpClient client = new DefaultHttpClient();
String url = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=pizza&zip=94306&results=2";
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
SAXReader reader = new SAXReader();
handleResponse(reader.read(bis));
} catch (Exception e) {
System.err.println(e);
}
}
private void handleResponse(Document doc) {
// doc is the XML response
// process the results here
}
There is no API for this in Android, sorry. There may be Web services you can use for this.
May be this could help..
Intent intent =new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q=Cafe"));
startActivity(intent);

Categories

Resources