Android: Getting Path to Thumbnail

The standard way on Android to get a thumbnail is to use MediaStore.Images.Thumbnails.getThumbnail or MediaStore.Video.Thumbnails.getThumbnail dependent if you want a thumbnail from an image or an video. Both of these methods return a Bitmap ready to use.

However, sometimes you may want to have the path to the thumbnail, e.g. because your own ContentProvider also need to provide thumbnails for files. The documentation of the thumbnail functionality is not that clear currently, but to build this functionality you can query the standard thumbnail content provider similiar to querying the media store for the real file.

To get the path of a mini thumbnail (MINI_KIND) for the image on the external storage you could do the following:

String getThumbnailPathForLocalFile(long fileId)
{
    Cursor thumbCursor = null;
    try
    {
        thumbCursor = getContext().getContentResolver().
                query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI
                , null
                , MediaStore.Images.Thumbnails.IMAGE_ID + " = " + fileId+ " AND "
                  + MediaStore.Images.Thumbnails.KIND + " = "
                  + MediaStore.Images.Thumbnails.MINI_KIND , null, null);

        if(thumbCursor.moveToFirst())
        {
            // the path is stored in the DATA column
            int dataIndex = thumbCursor.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA );
            String thumbnailPath = thumbCursor.getString(dataIndex);
            return thumbnailPath;
        }
    }
    finally
    {
        if(thumbCursor != null)
        {
            thumbCursor.close();
        }
    }

    return null;
}

Things to consider:

  • Querying the thumbnail content provider does not trigger the creation of the thumbnail. If the query does not return anything, you should trigger the creation of the thumbnail by calling the appropriate getThumbnail method and then querying the thumbnail content provider again.
  • Getting the path to the thumbnail only works for mini thumbnails (MINI_KIND) because only for these the Android system creates files to store the thumbnail. Micro thumbnails (MICRO_KIND) are stored in the database only. But at least you can get the raw data also by querying the thumbnail content provider as described above.

Interesting Google IO 2011 Sessions

Since I finally found the opportunity to have a look at some Google IO 2011 sessions, here are some links to sessions I can recommend:

Android Development Tools:
Shows some neat tips & tricks for using new (and old) features of the ADT plugin for Eclipse

Android Protips: Advanced Topics for Expert Android App Developers
Provides a lot of great tips and some patterns for common problems, e.g. keeping backward compatibility while still using new features, doing battery efficient data updates, etc.

Memory management for Android Apps
Background information about the garbage collector, memory management in the Dalvik VM and how to find memory problems by using the Eclipse Memory Analyzer

Building Aggressively Compatible Android Games
A good high-level summary of how to make apps compatible for many devices.

Android: Open dynamically created file with ContentProvider

If you have asked yourself how to can return a handle to a temporary file from an own Android ContentProvider implementation, here is a good solution I found at tomgibara.com by utilizing the behavior of the underlying linux file system:

The basic scenario is this: You have implemented a ContentProvider and you want to use it to return something that is too big to fit into a cursor, say an image. It’s clear from the relevant Android platform javadocs, though perhaps not from the general ContentProvider documentation, that this is done by implementing openFile and returning a ParcelFileDescriptor.

But now suppose that the image resource is being rendered on the fly. How do you implement openFile?

As per the contact of openFile you need to return a ParceFileDescriptor which means that the resource must be returned via an open file (or possibly a socket, which we’ll ignore). This means that the image must first be written to a file, which is then opened as a ParceFileDescriptor. The interesting question here is when do you delete the file?

You can’t wait until the calling process has finished reading the file because (a) you won’t get notified when that occurs and (b) it may never exhaust the stream anyway. You can’t rely on the calling process to delete the file for you either, also, the whole idea behind wrapping the resource in this way is to guard it from direct access by other processes. You could delete the file after a fixed time period — assuming that the caller will have finished reading the file by that time. But this is unnecessarily complex, because the answer turns out to be very simple:

You delete the file immediately after you’ve opened the ParcelFileDescriptor but before you return it from the openFile method.

The reason this works is down to the way that Linux filesystems operate: directories maintain links to files, when a process opens a file a new link is created, closing a file or removing it from a directory removes a link. When there are no links to a file, the file is deleted.

So by opening the file in our application, we create a link to it. Then ‘deleting’ the file actually unlinks it, but the file won’t really be deleted until the file descriptor is discarded (ie. the last link is removed). This will happen automatically at some point after the calling application has finished accessing the file and any associated ParcelFileDescriptor objects have become garbage.

Read the full post at Returning dynamically generated resources – Parentheticals & Excursions.

How to detect and avoid memory and resources leaks in .NET applications

MSDN features an article covering reasons why and how managed applications can suffer from memory leaks, how you can find them and get rid of them:

Despite what a lot of people believe, it’s easy to introduce memory and resources leaks in .NET applications. The Garbage Collector, or GC for close friends, is not a magician who would completely relieve you from taking care of your memory and resources consumption.

I’ll explain in this article why memory leaks exist in .NET and how to avoid them. Don’t worry, I won’t focus here on the inner workings of the garbage collector and other advanced characteristics of memory and resources management in .NET.

It’s important to understand leaks and how to avoid them, especially since they are not the kind of things that is easy to detect automatically. Unit tests won’t help here. And when your application crashes in production, you’ll be in a rush looking for solutions. So, relax and take the time to learn more about this subject before it’s too late.

How to detect and avoid memory and resources leaks in .NET applications weiterlesen

Tooltips for indivual cells of a DataGrid

Just recently,  I have been faced with the question on how to add cell dependent tooltips to individual cells of a DataGrid in Silverlight. If you generate the columns by yourself, the task is straight forward by using the appropriate data template for the cell. Something like this:

<Data:DataGrid>
   <Data:DataGrid.Columns>
      <Data:DataGridTemplateColumn>
         <Data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <TextBlock  Text="{Binding ColumnProperty}"
                    ToolTipService.ToolTip="{Binding ColumnProperty}"/>
            </DataTemplate>
         </Data:DataGridTemplateColumn.CellTemplate>
      </Data:DataGridTemplateColumn>
   </Data:DataGrid.Columns>
</Data:DataGrid>

However, if you let the columns be autogenerated by the grid, things are not so straight forward anymore. Tooltips for indivual cells of a DataGrid weiterlesen

WCF Services with Large Responds

If you happen to develop a WCF service that needs to deliver large amount of data to your Silverlight application, you might run into the problem that the data will never reach your application but instead you might just get an exception like „System.Net.WebException: The remote server returned an error: NotFound„.

There are two possible reasons for this:

  1. The transfer mode of your service being set to „Buffered“ and the size of the data being transferred is bigger than the buffer size.
  2. There are two many items to be serialized and deserialized by the DataContractSerializer.

WCF Services with Large Responds weiterlesen

Understanding WCF Services in Silverlight 2

NetFX Harmonics provides a in-depth and well written introduction on how to write and use WCF services in Silverlight applications:

In this document, I’m going to talk about how to access WCF services using Silverlight 2 without magic. There will be no proxies, no generated code, no 3rd party utilities, and no disgusting „Add Service Reference“ usage. Just raw WCF. This document will cover WCF connectivity in quite some depth. We will talk about service setup, various WCF, SOA, and Silverlight paradigms, client setup, some security issues, and a few supplemental features and techniques to help you aide and optimize service access. You will learn about various WCF attributes, some interfaces, and a bunch of internals. Though this document will be in depth, nothing will ever surpass the depth of MSDN. So, for a more full discussion on any topic, see the WCF documentation on MSDN.

Even though we’re focusing on Silverlight, most of what will be explained will be discussed in a .NET context and then applied to Silverlight 2. That is, instead of learning .NET WCF and Silverlight WCF, you will .NET WCF and how to vary this for Silverlight. This comparative learning method should help you both remember and understand the concepts better.

Read the full document here.

WPF Startup Performance

If you are developing a WPF application, you know about the increased startup time compared to native applications. One of the reasons is the time needed for loading the CLR the first time it is used after a reboot, but there are a lot more aspects affecting the time until your application window is shown on the screen and fully functional. Although .NET 3.5 SP1 improved the initialization time noticably, it is worth to understand the pitfalls and to design your application for fast startup and initialization. The following links will provide you some insights information: WPF Startup Performance weiterlesen

Developing for the Windows 7 Desktop

If you are currently investigating on how to integrate your applications into the Windows 7 desktop, the following links may be a good starting point:

Make sure that you are also familiar with the Application User Model IDs:

Application User Model IDs (AppIDs) are used extensively by the taskbar in Windows 7 and later systems to associate processes, files, and windows with a particular application. In some cases, it is sufficient to rely on the internal AppID assigned to a process by the system. However, an application that owns multiple processes or an application that is running in a host process might need to explicitly identify itself so that it can group its otherwise disparate windows under a single taskbar button and control the contents of that application’s Jump List.