Android setImageUri 不适用于资产 Uri

发布于 2024-12-06 14:05:54 字数 416 浏览 1 评论 0原文

我使用以下代码从资产文件夹中设置图像。

Uri numBgUri = Uri.parse("file:///android_asset/background_numbers.png");
numBgImage.setImageURI(numBgUri);

background_numbers.png 文件肯定存在于 asset 根目录中。我在日志中收到 FileNotFoundException: -

09-23 17:05:23.803: WARN/ImageView(23713): Unable to open content: file:///android_asset/background_numbers.png

有什么想法我可能做错了吗?

谢谢!

I am using the following code to set an image from the assets folder.

Uri numBgUri = Uri.parse("file:///android_asset/background_numbers.png");
numBgImage.setImageURI(numBgUri);

The background_numbers.png file definitely exists in the assets root directory. I am getting a FileNotFoundException in the log: -

09-23 17:05:23.803: WARN/ImageView(23713): Unable to open content: file:///android_asset/background_numbers.png

Any ideas what I could be doing wrong?

Thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽心 2024-12-13 14:05:54

我设法使用我自己的自定义 ContentProvider 找到了一个可行的解决方法(这比您想象的更容易!)。感谢Google 网上论坛上的skink提供的提示

此代码应该可以工作,您只需要设置一个提供程序 URL 并将其注册为清单文件中的提供程序

package sirocco.widgets;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;

public class AssetContentProvider extends ContentProvider {

    private AssetManager mAssetManager;
    public static final Uri CONTENT_URI = 
            Uri.parse("content://your.provider.name");

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {
        mAssetManager = getContext().getAssets();
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        String path = uri.getPath().substring(1);
        try {
            AssetFileDescriptor afd = mAssetManager.openFd(path);
            return afd;
        } catch (IOException e) {
            throw new FileNotFoundException("No asset found: " + uri);
        }
    }

}

我希望这可以帮助任何遇到同样问题的 Google 用户!

I managed to get a viable workaround for this using my own custom ContentProvider (It's easier than you think!). Thanks to skink on Google Groups for the tip

This code should work, you just need to set a provider URL and register it as a provider in the manifest file

package sirocco.widgets;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;

public class AssetContentProvider extends ContentProvider {

    private AssetManager mAssetManager;
    public static final Uri CONTENT_URI = 
            Uri.parse("content://your.provider.name");

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {
        mAssetManager = getContext().getAssets();
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        String path = uri.getPath().substring(1);
        try {
            AssetFileDescriptor afd = mAssetManager.openFd(path);
            return afd;
        } catch (IOException e) {
            throw new FileNotFoundException("No asset found: " + uri);
        }
    }

}

I hope this helps any googlers out there with the same problem!

小草泠泠 2024-12-13 14:05:54

您需要将所有图像放入资源目录中。例如:

/root/res/drawable/background_numbers.png

您可以通过以下方式访问它:

numBgImage.setImageResource(R.drawable.background_numbers);

You need to place all images in you resources directory. For example :

/root/res/drawable/background_numbers.png

And you can access it via:

numBgImage.setImageResource(R.drawable.background_numbers);
怼怹恏 2024-12-13 14:05:54

我建议您仔细检查:

  • logcat 中出现的 ImageView 活动与您的代码属于同一个 apk。资产文件只能从您的应用程序内读取。

  • 资产文件夹位于项目根目录中。

编辑

似乎不可能提供 ImageView 直接来自资产资源。请参阅此问题进行说明。
作为解决方法,您应该尝试从资源文件创建一个可绘制对象,然后使用 ImageView.setImageDrawable

Drawable d = Drawable.createFromStream(getAssets().open("background_numbers.png"), null);
if (null != d) numBgImage.setImageDrawable(d);

此代码的第一行是 这个答案来自@伊戈尔·霍缅科。

I advice you to double check that:

  • ImageView activity that appears in your logcat belongs to the same apk that your code. asset files are only readable from within your application.

  • The assets folder is in the project directory root.

EDIT

It seems that it is not possible to feed an ImageView directly from an asset ressource. See this question for illustration.
As a workaround you should try to create a drawable from your asset file, then associate it with your Image view using ImageView.setImageDrawable:

Drawable d = Drawable.createFromStream(getAssets().open("background_numbers.png"), null);
if (null != d) numBgImage.setImageDrawable(d);

The first line of this code is an adaptation of this answer from @IgorKhomenko .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文