Sunday, May 4, 2014

Downloading image blob from Google's App Engine

Last time we have seen how to upload an image to app engine. Now its time to fetch it back. There are 2 ways to do that.

1. Using serve()
2. Using urls generate for each blob.

I tried using 1st method, but it did not work. So tried out second method which did work. Following are the details of that implementation.

First get the url for download for each blob. To do that you need blob key of your blobs. In my case I have saved blob keys in a separate database entity. Blob key is returned when you create new blob.
        
String[] array = getBlobKeyByProblemId(problemId);
if (array != null && array.length > 0) {
    String[] urls = new String[array.length];
    for (int i = 0; array != null && i < array.length; i++) {
        ImagesService imagesService = ImagesServiceFactory.getImagesService();
        BlobKey key = new BlobKey(array[i]);
        ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(key).secureUrl(true);
        urls[i] = imagesService.getServingUrl(options);
    }
}

Send this key to client, so that whenever it needs to download image it will use this url.
 
Now time to get the image. Call the following method as:
returnValue = httpPostBinary( entry.url, null );

private byte[] httpPostBinary( String url, String data ) {
        if ( url == null )
            return null;
        
        int bufferSize = 1024;
        int loopCounter = 3;
        int timeout = 1 * 60 * 1000;
        byte[] retVal = null;
        HttpURLConnection urlConnection = null;
        while ( loopCounter > 0 ) {
            try {
                retVal = null;
                Log.d( Utils.AppName, "CommunicationManager.httpPostBinary(): Communication attempt#" + loopCounter );
                byte[] buffer = new byte[bufferSize];
                URL httpUrl = new URL( url );
                // handleTrustedConnection();
                urlConnection = (HttpURLConnection) httpUrl.openConnection( );
                urlConnection.setRequestMethod( "POST" );
                urlConnection.setRequestProperty( "User-Agent", "eRecommendation" );
                urlConnection.setRequestProperty( "Accept", "*/*" );
                urlConnection.setRequestProperty( "Content-Type", "application/xml" );
                if ( data != null )
                    urlConnection.setDoOutput( true );
                else
                    urlConnection.setDoOutput( false );
                urlConnection.setConnectTimeout( timeout );
                if ( data != null ) {
                    OutputStream outputStream = urlConnection.getOutputStream( );
                    if ( outputStream != null ) {
                        DataOutputStream dataOutputStream = new DataOutputStream( outputStream );
                        dataOutputStream.writeUTF( data );
                        dataOutputStream.close( );
                        outputStream.close( );
                    }
                }
                InputStream inputStream = urlConnection.getInputStream( );
                if ( inputStream != null ) {
                    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream( );
                    int count;
                    while ( true ) {
                        count = inputStream.read( buffer );
                        if ( count == -1 )
                            break;
                        arrayOutputStream.write( buffer, 0, count );
                    }
                    retVal = arrayOutputStream.toByteArray( );
                    arrayOutputStream.close( );
                    inputStream.close( );
                }
                if ( retVal != null && retVal.length > 0 )
                    break;
            } catch ( Exception e ) {
                Log.d( Utils.AppName, "CommunicationManager.httpPost():" + e.toString( ) );
                retVal = null;
            } finally {
                if ( urlConnection != null )
                    urlConnection.disconnect( );
            }
            try {
                Thread.sleep( 1000 );
            } catch ( InterruptedException e ) {
            }
            loopCounter--;
        }
        return retVal;
    }

retVal contains array bytes which forms the image. You can save this image in file system and use it.

No comments:

Post a Comment

Android aar deployment in Maven - 2022

Introduction If you are working on android library project, you might be wondering how to publish it on Maven like this . Earl...