I am building an app-engine endpoint api that takes picture from a user (android app) and saves it to blobstore programmatically. Then I save the blob_key in my datastore. The code goes like this:
First I received the image through my #endpoint.method as a messages.BytesField:
image_data = messages.BytesField(1, required=True)
Then I save to the blobstore like this:
from google.appengine.api import files
def save_image(data):
# Create the file
file_name = files.blobstore.create(mime_type='image/png')
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write('data')
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
return blob_key # which is then saved to datastore
Now I want to serve the image back. I don't see how to fit the following code into my endpoints api:
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
In the end I imagine a serving procedure like this:
in #endpoints.method:
get blob_key from datastore
obtain image with blob_key
add image to StuffResponseMessage
send StuffResponseMessage to front-end (android app)
My approach is because I want to protect the privacy of my users. Any thoughts on how to do this well? My code snippets are generally from the google developer tutorial.
EDIT:
I don't see how I would pass the blob_key from the datastore to the following method to retrieve the image:
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
What's inside resource, anyway?
I believe resource is the BlobKey object of what you want to serve, as a string, retreived from url path. If you look at the source of google.appengine.ext.blobstore.BlobInfo the get method uses a function __normalize_and_convert_keys that takes an argument of BlobKey object OR string.
If the blob is an image, maybe it's best to send the serving url instead to your Android app, in StuffResponseMessage maybe in your case. From google's Serving a Blob doc:
If you are serving images, a more efficient and potentially less-expensive method is to use get_serving_url using the App Engine Images API rather than send_blob. The get_serving_url function lets you serve the image directly, without having to go through your App Engine instances.
So after you save the image, take the returned blob_key (as per your method save_image(data) above) to make a serving url, then in your Android app get the image from the returned url. That of course means exposing the image url without privacy protection.
If you want to do it with protection, use BlobReader class to read the blob with file like interface. You can't use the method/class from Serving a Blob example because you do it in a remote.Service subclass rather than a handler.
Related
I'm working with a tutorial fromRay Wenderlich (https://www.raywenderlich.com/124438/android-listview-tutorial). In the tutorial, an image is retrieved via the Internet using a URL specified in the locally stored JSON file:
"image" : "https://www.edamam.com/web-img/341/3417c234dadb687c0d3a45345e86bff4.jpg"
The string is stored in the imageUrl variable:
recipe.imageUrl = recipes.getJSONObject(i).getString("image");
Then the image is loaded using Picasso:
Picasso.with(mContext).load(recipe.imageUrl).placeholder(R.mipmap
.ic_launcher).into(thumbnailImageView);
I would like to change the code in the tutorial so that the image is retrieved from the drawable folder within the app, rather than via the Internet. I've been working on this on and off for several days. I assumed it was a matter of changing the URL in the JSON data so that it specified a path to the image file in the drawable folder like this:
"image" : "android.resource://com.raywenderlich.alltherecipes/drawable/chicken.jpg"
So far I've been unsuccessful. Am I on the right path, or am I way off? I'm an Android newbie. I'm used to working with plists in Xcode (although I'm no expert there, either).
In my project i need to include searchs to a API Rest to query with a url that points to a jpg picture and it returns a list of urls with similar pictures.
Google Images performs via web like i am looking for, but the clients for Android that i have found (github projects) they work simply with an input text.
The simplest code would be something as below:
String inputUrlPic = "https://cdn.pixabay.com/photo/2014/03/14/20/13/dog-287420__340.jpg";
String[] returnedUrlPictures = null; //list of returned pictures similars to inputUrlPic
ReverseImageApi reverseImageApi = new ReverseImageApi();//My desired Api object
returnedUrlPictures = reverseImageApi.findSimilarPicturesFromUrl(inputUrlPic); //Ok, it would be an asynctask
So, my question is, exists a API or an Android client API to perform in this way?
I have to upload an image from Android device to a web app written in Python and Django. For storing images, I have made use of easy thumbnails.
What I have done so far is convert the image to base64 string to and post it to server. Well this is working perfectly, also if I write the base64 string to png image file it goes smoothly.
Now, I want to save it to the database as far as I know, easy thumbnails save the actual file to some location on server within the app file structure, and saves a link to same in database.
I am not understanding, how do I save the base64 string I receive in POST to my database.
My Model:
class Register(models.Model):
image = ThumbnailerImageField(upload_to=filename, blank=True, null=True, max_length=2048)
def filename(inst, fname):
f = sha256(fname + str(datetime.now())).hexdigest()
return '/'.join([inst.__class__.__name__, f])
The image is not stored in the db. It is stored to disk. Only a reference to the file is stored in the db.
def Article(models.model):
image = models.ImageField(...)
Somewhere you create a new object:
from django.core.files.base import ContentFile
obj = Article()
data = base64.b64decode(encoded)
# You might want to sanitise `data` here...
# Maybe check if PIL can read the `data`?
obj.image.save('some_file_name.ext', ContentFile(data))
obj.save()
do you know a method or the method used by the evernote widget to retrieve the thumbnails that we see in a very convenient way (before retrieving the entire note) in the main interface or in the widget?
I saw the post method via http request, but it seems complicated when not sharing notes and perhaps there are more straightforward methods via a direct evernote API call or via reading files stored by the application(s)?
The widget pulls the thumbnails from the Evernote app's Content Provider.
Something like this should work.
In your manifest :
<permission android:name="evernote.permission.READ_DATA" android:protectionLevel="normal" />
In your java code:
final Uri AUTHORITY_URI = Uri.parse("content://com.evernote.evernoteprovider");
final Uri NOTE_URI = Uri.withAppendedPath(AUTHORITY_URI, "notes");
private FileDescriptor getNoteThumbnail(Context context, String noteGuid) throws FileNotFoundException {
Uri thumbnailUri = NOTE_URI.buildUpon().appendEncodedPath(noteGuid).appendPath("thumbnail").appendPath("data").build();
ContentResolver cr = context.getContentResolver();
return cr.openFileDescriptor(thumbnailUri, "r").getFileDescriptor();
}
The HTTP Post method is not too complex. I'm not familiar with Java but this is an example in python that should be pretty straightforward to port to android:
import requests
ACCESS_TOKEN="INSERT YOUR AUTH TOKEN OR DEV TOKEN HERE"
payload={'auth':ACCESS_TOKEN} #map auth token to param "auth"
r=requests.post('https://sandbox.evernote.com//shard/s1/thm/note/e679c010-d8b2-4644-9eag-56bd31c84be7.jpg?size=75',data=payload, stream=True) #returns a binary of the picture type in header
f=open('thumbnail.jpg','wb') #open file for binary writing
f.write(r.content) #write binary contents of request to a file
f.close() #close the file
The only parameter of the POST request is "auth" and it should contain your auth token (or dev token). The rest of the information comes from the URL itself and is of the form:
[domain].evernote.com/shard/[shard]/thm/note/[guid]
where
[domain] is sandbox (for the sandbox) and www (for production)
[shard] is the shard the account is on (should be something like s1)
[guid] is the notebook guid
with the optional parameters appended at the end of .jpg, .gif, .bmp or .png as well as the optional parameter at the end of the URL ?size=[1 to 299] (default is 300px square)
for example on the sandbox with shard s1, note guid "e669c090-d8b2-4324-9eae-56bd31c64af7" to return a jpg of size 150px square the URL would look like this:
https://sandbox.evernote.com/shard/s1/thm/note/e669c090-d8b2-4324-9eae-56bd31c64af7.jpg?size=75
I have yet another pesky question for people who understand how Google Drive SDK works. On Android platform, I am creating my own custom thumbnails for JPEG image files ( thumbnail is a reduced JPG of the most important detail of the parent image ), The size is a bit non-standard - 384 x 128px, but well within limits stated in the documentation. So, the code goes like this:
// thumbnail
String myThumb = "test.tnl";
Thumbnail tn = new Thumbnail();
tn.setMimeType("image/jpeg");
tn.setImage(Base64.encodeBase64String(myThumb.getBytes()));
// define meta-data
File body = new File();
body.setTitle("test.jpg");
body.setDescription("bla bla");
body.setMimeType("image/jpeg");
body.setThumbnail(tn);
File gooFl = drvSvc.files()
.insert(body, new FileContent("image/jpeg", new java.io.File(test.jpg)))
.execute();
and executes flawlessly (there are more 'body' elements I don't list here) and everything works like a charm. But when I download the image, my thumbnail is gone, replaced by standard Google thumbnail - s220 type.
I did notice the documentation statement:
As with indexable text, Drive automatically generates thumbnails for many common file types. For shortcuts and other file types Drive can not render, you can provide a thumbnail image generated by your application.
Reading it ambiguously, I was hoping that by supplying my own thumbnail to a known MIME type, I will keep Google Drive from generating its standard one, but it probably is not the case. So the question remains. Is there a solution to my problem? Having custom thumbnails for standard "image/jpeg" MIME types? Or is there a work-around, perhaps another custom field I can stick some 10Kb of binary data in? I need the thumbnails in my Android viewer - another app.
Thank you, sean
Google Drive will only use custom thumbnails for non-standard MIME types, so you can't override the one for jpeg. Your app can use a custom file property to store and read proprietary data:
https://developers.google.com/drive/properties