I am designing a Image Puzzle(JigSaw) game. I am stuck in a problem that, when I start the game it shows the option "New Image" but, when I click on that, it gives an error message, means images from the drawable folder are not loading.
I did research and i came with this, i have implemented this in my code. but, it gives an error.. "Could not Load."
protected void newImage()
{
LinkedList<Uri> availableImages = new LinkedList<Uri>();
findInternalImages(availableImages);
if(availableImages.isEmpty())
{
Toast.makeText(this, getString(R.string.error_could_not_load_image), Toast.LENGTH_LONG).show();
return;
}
int index = new Random().nextInt(availableImages.size());
loadBitmap(availableImages.get(index));
}
protected void findInternalImages(List<Uri> list)
{
Field[] fields = R.drawable.class.getFields();
for(Field field: fields)
{
//String name = field.getName();
//if(name.startsWith(FILENAME_IMAGE_PREFIX))
//{
//int id = getResources().getIdentifier(name, "drawable", getPackageName());
for(int j=1;j<13;j++)
{
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier("image_"+j, "drawable", getPackageName()));
}
//}
}
}
My Problem is,
when i run the program it should show images from drawable folder and when i choose the one then actual scrambling should start. so, one can play the jig saw game.
Start by checking that your images are in a Android compatible format, that was causing me problems to begin with. I normally stick to .png now.
This is the code you will need to modify the image
ImageView pwrSwap = (ImageView) beaconitems.findViewById(R.id.yourImageID);
pwrSwap.setImageResource(R.drawable.yourImage);
http://developer.android.com/guide/appendix/media-formats.html
Related
I'm downloading files with my app via an API (this is working perfectly). Later in my app I'm doing a call to my database to get the file info: id, name, location and return it as a HashMap:
HashMap<Integer, ArrayList<String>> imgTitle;
I take the returned HashMap, extract the ArrayList and use that to populate some buttons. the array list holds the filename and file location. When the page loads the names display correctly but the images don't always load or one loads. If I go back and reenter the activity (via a button press) I'll get different images showing up. I've sent my array to the log and the file location is present and correct for all arrays. Why are only some image showing?
for (Map.Entry<Integer, ArrayList<String>> e : imgTitle.entrySet()) {
...
ArrayList<String> catList = e.getValue();
final String catTitle = catList.get(0);
File indexImage = new File(catList.get(1));
// add images
ImageButton imgButton = new ImageButton(this);
imgButton.setImageURI(Uri.fromFile(indexImage));
...
}
The above is inside a method called on the onCreate. My assumption is maybe the setImageURI is too slow? Any ideas?
Prior to using the setImageURI I used
imgButton.setImageResource(R.drawable.test_vehicle);
as a placeholder it was working fine. However, now I have to use a file that was downloaded and saved locally.
You could try to decode a Bitmap from a stream, like this:
File indexImage = new File(catList.get(1));
InputStream imageStream = new FileInputStream(indexImage);
Bitmap backgroundImage = null;
try {
backgroundImage = BitmapFactory.decodeStream(imageStream);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
//Dispose of the temporary resources
imageStream.close();
imageStream = null //So that the stream is deleted on next GC
}
catch(IOException e) {
e.printStackTrace();
}
}
if (backgroundImage != null) {
ImageButton imgButton = new ImageButton(this);
imgButton.setImageBitmap(backgroundImage);
backgroundImage.recycle();
}
Note: This is pure speculation, I haven't tested any of this. It's simply the first solution that came to my mind
I'm currently trying to start a thread which fetches a drawable from its resources and posts it after a set amount of time. Then fetches the next drawable from resources and does the same thing all over.
final ImageView zImageView = (ImageView) findViewById(R.id.zzz_animation_view);
new Thread(new Runnable() {
public void run() {
for(int i = 1; i<=510; i++)
{
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.z_animation0001); // how do I increment this recourse to z_animation0002, 0003, ..., 0510 ?
zImageView.postDelayed(new Runnable() {
public void run() {
zImageView.setImageBitmap(bitmap);
}
},200);
}
}
}).start();
Firstly. Is this a proper way of approaching the problem? The drawables are too large for me to use animation-list and as such my goal is to load the images one at a time to ensure that I have enough memory. Secondly how do I solve the problem of iterating over my resources?
You can get resource id by its name using Resources.getIdentifier :
final int current = 5;
final String pattern = "z_animation%04d";
final String name = String.format(pattern, current);
final int id = getResources().getIdentifier(name, "drawable", getPackageName());
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
Also, here you can find my answer for the same question with code sample that does exactly what you want.
I would put the images in the assets folder and load them from there as described here: https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
In the assets folder you can just add a number to the file name.
My problem is the following: I try to load images to a text view like this:
URLImageParser p = new URLImageParser(textView, this);
Spanned htmlSpan = Html.fromHtml(textWithImages, p, null);
textView.setText(htmlSpan);
I followed this example https://stackoverflow.com/a/7442725/1835251
If I load images from the web it works perfectly.
I modified to code a bit to load certain images from SDCard as well.
In the "doInBackground" Method of the async task I implemented this:
#Override
protected Drawable doInBackground(String... params) {
String source = params[0];
Drawable d = null
if(source != null) {
if(source.startsWith("/myApp/images/")) {
localImage = true;
}
}
if(localImage) {
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
d = Drawable.createFromPath(sdcard+source);
d.setBounds(0, 0, 0 + (int)(d.getIntrinsicWidth()), 0
+ (int) (d.getIntrinsicHeight()));
localImage = false;
} else {
// load images from internet
}
return d;
localImage is a boolen which is set to determine if the current source "points" to an
image from the internet or a local image.
As stated before: Loading images from the internet works perfectly.
BUT when I load images from the SDCard it sometimes happens that not all images are displayed.
The whole text (including the images) gets cut as if it hasn't been loaded correctly.
I figured out that this happens much more often on a Samsung Galaxy S3 than on a Samsung S Plus.
On the S3 I sometimes load only 1 or 1.5 images and the rest gets cut.
The S Plus always loads all 4 images but rarely cuts the last 2 or 3 sentences of the text.
I think that it is a sort of a timing problem with the AsyncTask but I never worked with it before.
I know this is a really large post but I hope that someone still can help me.
Best regards
Siggy
Ok I am stupid... I solved it!
I forgot that I only need the AsyncTask if I load images from the internet. For local images I don't need that.
Solution for the interested:
ImageGetter imgGetter = new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
Drawable d = Drawable.createFromPath(sdcard+source);
d.setBounds(0, 0, 0 + (int)(d.getIntrinsicWidth()), 0
+ (int) (d.getIntrinsicHeight()));
return d;
}
};
Spanned htmlSpan = Html.fromHtml(textAndImage, imgGetter, null);
textView.setText(htmlSpan);
Does anyone know of a way to tell which folder Android system will resolve to when I request a particular drawable from the Resources (drawable-long vs. drawable-notlong, for example)? I want to make sure it's loading resources the way I would expect it to (for instance, to make sure the Tattoo is really ldpi and not mdpi since it's right on the borderline).
I could always print out the intrinsic size of the drawable after I fetch it from Resources, and check that against the various image sizes I have, but that's kind of a pain. Plus, my company allows its clients to skin their app before it's released, so after it's been skinned I won't be able to rely on that method.
I tried using Resources.getName(resId) but that doesn't give the folder name, and as far as I know you can get a AssetFileDescriptor for the resource but you can't get an actual filename.
UPDATE:
Since asking this question I have discovered that you can access many of the resource qualifier names through the Configuration and DisplayMetrics classes (both accessible via getResources()). DisplayMetrics can give you the ldpi/mdpi/hdpi designation, and Configuration can give you small/normal/large screen size, long/notlong aspect ratio, locale, mobile network codes, and other hardware information. However, this still does not positively confirm that you are getting the resources you expect if you have a complicated resource set. It also does not protect you from bugs that report the incorrect values, like the Motorola Droid bug where the DisplayMetrics reports the wrong xdpi and ydpi (it reports 96dpi where the real value is more like 265dpi! Terrible bug.).
A different approach would be to use two different images, with the same name, red in one resource folder blue in the other. When it inflates, you'll be able to see which one it is coming from.
If you really want to go crazy, make an image saying "ldpi" or "xhdpi" etc for each folder and when you start your project, you can note which ones came up for each device you wanted explicit testing for.
From Mark Allison, same idea but the values directory with strings that contains the name of the bucket it's in - it's less effort than creating images!
I know, you write, that you can go through all resources and compare sizes with the bitmap size. But I don't know, if you thought it same way as I did, so this may help you or someone else.
So this is the way I did it. At first I load sizes (in bytes) for all drawable images.
String defType = "drawable";
String drawableDirPrefix = "res/drawable";
SparseArray<SparseArray<String>> resourceDirs = new SparseArray<SparseArray<String>>();
ZipInputStream apk = null;
try {
apk = new ZipInputStream(new FileInputStream(getPackageResourcePath()));
ZipEntry entry = null;
while ((entry = apk.getNextEntry()) != null) {
String resourcePath = entry.getName();
if (resourcePath.startsWith(drawableDirPrefix)) {
int firstSlashPos = resourcePath.indexOf('/');
int lastSlashPos = resourcePath.lastIndexOf('/');
int dotPos = resourcePath.lastIndexOf('.');
String resourceDir = resourcePath.substring(firstSlashPos + 1, lastSlashPos);
String resourceName = resourcePath.substring(lastSlashPos + 1, dotPos);
int resourceId = getResources().getIdentifier(resourceName, defType, getPackageName());
int resourceSize = (int) entry.getSize();
SparseArray<String> resourceInfo = resourceDirs.get(resourceId);
if (resourceInfo == null) {
resourceInfo = new SparseArray<String>();
resourceInfo.append(resourceSize, resourceDir);
resourceDirs.append(resourceId, resourceInfo);
} else {
resourceInfo.append(resourceSize, resourceDir);
}
}
}
} catch (IOException e) {
Log.e("tag", "Error", e);
} finally {
if (apk != null) {
try {
apk.close();
} catch (IOException e) {
Log.e("tag", "Error", e);
}
}
}
Then, when I want to know the folder, I can compare the bitmap size to loaded sizes.
InputStream bitmapStream = null;
try {
int bitmapId = R.drawable.icon;
bitmapStream = getResources().openRawResource(bitmapId);
int bitmapSize = bitmapStream.available();
String bitmapDir = resourceDirs.get(bitmapId).get(bitmapSize);
Log.i("tag", bitmapDir);
} catch (Exception e) {
Log.e("tag", "Error", e);
} finally {
if (bitmapStream != null) {
try {
bitmapStream.close();
} catch (IOException e) {
Log.e("tag", "Error", e);
}
}
}
Well, this will work only if the images have different sizes. Or you can compare other things, like width, height, etc.
May be it is not, what you are looking for, but at least it satisfied my needs.
I have the following problem:
suppose that until now, I am using R.drawable.img to set the image in some imageviews
with
imgView.setImage(aProduct.getImgId());
the simplified product class look like this:
class Product{
int imgId;
....
public void setImgId(int id){
this.imgId=id;
}
public int getImgId(){
return this.imgId
}
...
}
my application is now "evolved" because the user can add customized products
taking the img from the camera and getting the Uri of the picture.
and to set the image on the ImageView imgView.setImgURI(Uri)
Now my question is:
what would be the best approach to have a mixed int/Uri image resources ambient?
can I obtain the Uri of a "R.drawable.img"?
I'm not sure if my question is clear, I mean:
I have to check, before to set the imageview, if my product has an Uri or an int Id,
and then make an "if" to call the appropriate method, or there is a simpler solution?
Thank you for reading, and sorry for my english.
Regards.
Your problem is that there are basically 3 types of image resources:
R.id... resources: internal resources, such as icons you put into the res folder
content URI's: local files or content provider resources such as content:// or file:///sdcard/...
remote file URL's: images on the web, such as http://...
You are looking for a way to pass around one identifier that can deal with all three. My solution was to pass around a string: either the toString() of the URI's, or just the string respresentation of the R.id integer.
I'm using the following code, for example, to deal with this and make the code work with it:
public static FastBitmapDrawable returnAndCacheCover(String cover, ImageRepresentedProductArtworkCreator artworkCreator) {
Bitmap bitmap = null;
Uri coverUri = null;
boolean mightBeUri = false;
//Might be a resId. Needs to be cached. //TODO: problem: resId of default cover may not survive across versions of the app.
try {
bitmap = BitmapFactory.decodeResource(Collectionista.getInstance().getResources(), Integer.parseInt(cover));
} catch (NumberFormatException e) {
//Not a resId after all.
mightBeUri=true;
}
if(bitmap==null || mightBeUri){
//Is not a resId. Might be a contentUri.
try {
coverUri = Uri.parse(cover);
} catch (NullPointerException ne) {
//Is null
return null;
}
}
if(coverUri!=null){
if(coverUri.getScheme().equals("content")){
//A contentUri. Needs to be cached.
try {
bitmap = MediaStore.Images.Media.getBitmap(Collectionista.getInstance().getContentResolver(), coverUri);
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
}
}else{
//Might be a web uri. Needs to be cached.
bitmap = loadCoverFromWeb(cover);
}
}
return new FastBitmapDrawable(bitmap);
}
You might be interested to take over the logic part. Ofcourse cover is the string in question here.
Forget android.resource:// as a replacement for the R.id... integer. Claims are going round it does not work: http://groups.google.com/group/android-developers/browse_thread/thread/a672d71dd7df4b2d
To find out how to implement loadCoverFromWeb, have a look around in other questions or ping me. This web stuff is kind of a field of it's own.
(Based on GPLv3 code out of my app Collectionista: https://code.launchpad.net/~pjv/collectionista/trunk)