android - ContentProvider:使用 ContentProvider.openFile 传递的文件设置文件名时出现问题

发布于 2024-10-26 19:59:30 字数 1800 浏览 3 评论 0原文

我在实现 ContentProvider 以传递存储在 android 项目中应用程序的私有数据区域中的图像时遇到以下问题:

下面的实现返回的文件具有存储图像的完整绝对路径作为文件名,如下所示

_data_data_com.mypackage.imageprovider_app_images_123456789.jpg

其中 app_images 是应用程序存储图像的目录名称,123456789.jpg 是实际文件名。

我现在的问题是:如何使 ContentProvider 仅设置所传递图像的实际文件名(或者我指定的文件名)?

这应该是可能的,因为例如 android 中的内置提供程序设法仅提供图像的实际文件名。

数据库表包含 _id、文件名和 _data 列,其中 _data 保存文件系统中图像的绝对路径。

非常感谢任何提示:)

提前致谢,snowcrash123

以下是我当前 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;
    }
}

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 providers 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文