开发者

android - ContentProvider: problem setting filename for files delivered by using ContentProvider.openFile

开发者 https://www.devze.com 2023-02-20 00:32 出处:网络
I\'m facing the following problem while implementing a ContentProvider to deliver images stored in the private data area of an application in an android-project:

I'm facing the following problem while implementing a ContentProvider to deliver images stored in the private data area of an application in an android-project:

The files returned by the implementation below have the complete absolute path to the stored images as filename which look like

_data_data_com.mypackage.imageprovider_app_images_123456789.jpg

where app_images is the name of the directory the images are stored in by the application and 123456789.jpg is the actual filename.

My question now is: How can I make the ContentProvider set only the actual filename (or alternatively a filename I specify) for the delivered images?

It should be possible, since e.g. the built-in provider开发者_JAVA百科s in android manage to deliver only the actual filename for an image.

The database table has columns for _id, filename and _data, where _data holds the absolute path to the image in the filesystem.

Any hints are very highly appreciated :)

Thanks in advance, snowcrash123

Here are the relevant parts of my current ContentProvider:

@Override
public Cursor query(Uri uri, String[] projection, String selection, 
    String[] selectionArgs, String sortOrder)
{
    final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(PhotosTable.TABLE_NAME);

    switch (URI_MATCHER.match(uri))
    {
        case PHOTO_DIR:
            if (sortOrder == null)
            {
                sortOrder = PhotosDirectory.DEFAULT_SORT_ORDER;
            }
            break;

        case PHOTO_ID:
            queryBuilder.appendWhere(IPhotoColumns.FILENAME + "=" 
                + uri.getPathSegments().get(1) + " AND ");
            break;

        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }

    if (!mDb.isOpen())
    {
        assignWritableDb();
    }

    return queryBuilder.query(mDb, PhotosDirectory.ALL_COLUMNS, 
        selection, selectionArgs, null, null, sortOrder);
}

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
{
    if (URI_MATCHER.match(uri) != PHOTO_ID)
    {
        throw new IllegalArgumentException(
            "operation only permitted for single file");
    }
    try
    {
        return openFileHelper(uri, mode);
    }
    catch (FileNotFoundException e)
    {
        return null;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号