I am u开发者_StackOverflow中文版sing 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!
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!
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);
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 .
精彩评论