14

I'm trying to write an Android app that would browse the files on a phone. When I run the app on the emulator, I get the following errors:

02-18 22:14:17.839: E/AndroidRuntime(896): FATAL EXCEPTION: main
02-18 22:14:17.839: E/AndroidRuntime(896):     android.content.res.Resources$NotFoundException: Resource ID #0x0
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.content.res.Resources.getValue(Resources.java:1018)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.content.res.Resources.getDrawable(Resources.java:663)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.example.App.FileArrayAdapter.getView(FileArrayAdapter.java:56)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.fillDown(ListView.java:672)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.fillFromTop(ListView.java:732)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.ListView.layoutChildren(ListView.java:1625)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.View.layout(View.java:11278)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.os.Looper.loop(Looper.java:137)
02-18 22:14:17.839: E/AndroidRuntime(896):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-18 22:14:17.839: E/AndroidRuntime(896):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:14:17.839: E/AndroidRuntime(896):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-18 22:14:17.839: E/AndroidRuntime(896):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-18 22:14:17.839: E/AndroidRuntime(896):  at dalvik.system.NativeStart.main(Native Method)

Here's the java code for FileArrayAdapter.java:

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileArrayAdapter extends ArrayAdapter<Item>{

private Context c;
private int id;
private List<Item> items;

public FileArrayAdapter(Context context, int textViewResourceId, List<Item> objects)
{
    super(context, textViewResourceId, objects);
    c = context;
    id = textViewResourceId;
    items = objects;
}

public Item getItem(int i)
{
    return items.get(i);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View v = convertView;

    if(v == null)
    {
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(id, null);
    }

    final Item i = items.get(position);

    if(i != null)
    {
        TextView t1 = (TextView)v.findViewById(R.id.TextView01);
        TextView t2 = (TextView)v.findViewById(R.id.TextView02);
        TextView t3 = (TextView)v.findViewById(R.id.TextViewDate);

        ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
        String uri = "drawable/" + i.getImage();

        int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
        Drawable image = c.getResources().getDrawable(imageResource);
        imageCity.setImageDrawable(image);

        if(t1 != null)
            t1.setText(i.getName());

        if(t2 != null)
            t2.setText(i.getData());

        if(t3 != null)
            t3.setText(i.getDate());
    }

    return v;
}

}

UPDATE Here's the Item.java code:

public class Item implements Comparable<Item>{

private String name, data, date, path, image;

public Item(String n, String d, String dt, String p, String i)
{
    name = n;
    data = d;
    date = dt;
    path = p;
    image = i;
}

public String getName()
{
    return name;
}

public String getData()
{
        return data;
}

public String getDate()
{
        return date;
}

public String getPath()
{
        return path;
}

public String getImage() 
{
        return image;
}

@Override
public int compareTo(Item another) {
    // TODO Auto-generated method stub
    if(this.name != null)
        return this.name.toLowerCase().compareTo(another.getName().toLowerCase());
    else
        throw new IllegalArgumentException();
}

}
halfer
  • 19,824
  • 17
  • 99
  • 186
user2201650
  • 527
  • 7
  • 13
  • 28

3 Answers3

6

This code is causing the problem:

String uri = "drawable/" + i.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());

There are at least 2 problems here:

  1. The uri should not have a 'drawable' prefix
  2. The call to getIdentifier should pass 'drawable' as the resource type

So, try this code:

String uri = i.getImage();
int imageResource = c.getResources().getIdentifier(uri, "drawable", c.getPackageName());

NOTE: You did not post Item class, or what getImage does. If you are still getting the exception (because imageResource is 0) it is because you do not have a drawable resource named with the same exact name as what i.getImage() returns, so you'll need to check that also.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • I updated my question above with `Item.java` code. I tried what you told. But I still keep getting the same exception. – user2201650 Feb 19 '15 at 05:17
  • Did you read my note? What is getImage() returning exactly? Do you have a drawable resource with the exact same name (minus extension)? You need to post more information or just figure it out – Greg Ennis Feb 19 '15 at 13:55
  • the getImage() is returning a String from the Item.java file – user2201650 Feb 19 '15 at 19:58
  • Right... And apparently that string does not match the name of a resource! That's why it is returning zero! – Greg Ennis Feb 19 '15 at 20:00
  • I'm following this [link] (http://custom-android-dn.blogspot.com/2013/01/create-simple-file-explore-in-android.html) for the coding – user2201650 Feb 19 '15 at 20:18
  • Thank you! But I still keep getting the same exception. Can you help me here? – user2201650 Feb 19 '15 at 21:04
  • Tell me what is the value of the string returned by get image() and then tell me every file in your drawable directories – Greg Ennis Feb 19 '15 at 21:09
  • I have a PNG file named "ic_launcher.png" in the drawable-hdpi/mdpi/xhdpi folders. And I'm trying to print the `uri` before the line: 56. And I do not get any output onto the console. – user2201650 Feb 19 '15 at 21:21
  • If you are trying to print it and not getting any output that means its returning null or an empty string. Try hard coding "ic_launcher" and make sure that part works first – Greg Ennis Feb 20 '15 at 00:56
5

Don't worry . . .

I got the same problem before .Use my way to get drawable

 Drawable image = c.getResources().getDrawable(R.drawable.name_of_image_in_drawableFolder);

example :

Drawable image = c.getResources().getDrawable(R.drawable.ic_launcher);

This will definetely solove your problem.

But, If you dont have images in drawable folder There might be two posibilities

- Getting Drawable from Phone(SDCARD)
- Getting Drawable from url

In First Case

imageCity.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorage‌​Directory()+"image.png"));

In Second case

imageCity.setImageDrawable(drawableFromUrl("url")); 
    public Drawable drawableFromUrl(String url) throws IOException {
        Bitmap x;
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(x);
    }

Reference :Android Drawable Images from URL

If any problem persists you can contact me megaganpreet0@gmail.com .

Community
  • 1
  • 1
Gaganpreet Singh
  • 886
  • 1
  • 9
  • 20
  • I do not have a`drawable` folder in my Eclipse Project. – user2201650 Feb 19 '15 at 05:19
  • If you are getting image from SDCARD(PHONE) then use code below imageCity.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"image.png")); – Gaganpreet Singh Feb 19 '15 at 05:52
  • The other posibility is getting it from URL. imageCity.setImageDrawable(drawableFromUrl("url")); public Drawable drawableFromUrl(String url) throws IOException { Bitmap x; HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.connect(); InputStream input = connection.getInputStream(); x = BitmapFactory.decodeStream(input); return new BitmapDrawable(x); } reference:http://stackoverflow.com/questions/3375166/android-drawable-images-from-url – Gaganpreet Singh Feb 19 '15 at 06:16
  • I'm following this [link] (http://custom-android-dn.blogspot.com/2013/01/create-simple-file-explore-in-android.html) Can you change the code in that link and explain on how to do it? Thank you! :) – user2201650 Feb 19 '15 at 20:56
  • Hey! The code that you gave me i.e., `Drawable image = c.getResources().getDrawable(R.drawable.ic_launcher);` worked for me. I no longer get the exception. But for all the files/folder in the phone and SD card, I have the same image (the `ic_launcher`). Is there a way to get the specific image for a particular file/folder? For example say, a folder icon for a folder? – user2201650 Feb 19 '15 at 22:07
  • Use imageCity.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorage‌​Directory()+"image.png")); for getting file from memory card (read my full answer) – Gaganpreet Singh Feb 20 '15 at 11:28
  • Write your file address in place of - Environment.getExternalStorage‌​‌​Directory()+"image.png" – Gaganpreet Singh Feb 20 '15 at 11:30
3

This error also appear when showing an AlertDialog in Android when it uses applicationContext.

val builder = AlertDialog.Builder(applicationContext)

the solution is to use this:

val builder = AlertDialog.Builder(this)
brasofilo
  • 25,496
  • 15
  • 91
  • 179
hertzon
  • 89
  • 4