3

I have some android project and I am trying to read some file from the "res/drawable-xhdpi" directory so I put

InputStream is = null;

        try {
            is = getResources().openRawResource(R.drawable.my_btn); //my_btn is inside drawable-xhdpi directory
        } catch (Exception e) {
            e.printStackTrace();
        }

but I get

error opening trace file: No such file or directory 

How can I deal with this issue. I tried to read this directly from FileInputStream with an absolute path, then I realized that the root from which the files are fetched is the root of the JVM so I still got the same No such file or directoryerror message

Update :

I added an assets directory in side src/main, then I put my_btn.png inside it then I call inside my MainActivity class

InputStream is = null;

        try {

            is = getAssets().open("my_btn.png");

        } catch (Exception e) {
            e.printStackTrace();
        }

but I still get the same error

user1611830
  • 4,749
  • 10
  • 52
  • 89
  • What problem are you trying to solve this way? This is not how you use drawable resources. `openRawResource()` is for raw resources, not drawable resources. – CommonsWare Jul 24 '14 at 11:51
  • I am just trying to test and I take a picture and send it to `Parse` – user1611830 Jul 24 '14 at 11:57
  • that is being more complex than it should, probably because the base approach for doing it is wrong. So as a suggestion, edit your question telling us what you want to achieve, so we can find a more suitable answer. – Budius Jul 24 '14 at 12:15

3 Answers3

3

The drawable will be fetched from "res/drawable-xhdpi" only if you are running the app on an xhdpi device. Else it will try to fetch the drawable from the corresponding drawable folder according to the screen and if it is not there, you will get the same error as above. If you are not sure which screen size you are using the best work around is to create a directory named "drawable" inside the res folder(ie, "res/drawable") and then add my_btn into the newly created "drawable" folder. Then it will fetch the image from the new folder regardless of the screen size.

EDIT

You are using the raw folder, not the resource folder. openRawResource reads from the raw folder. Use the following code to get a drawable and convert it to input stream(You will have to surround with try-catch).

Drawable drawable = getResources().getDrawable(R.drawable.my_btn);
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream); //use the compression format of your need
InputStream is = new ByteArrayInputStream(stream.toByteArray());
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
2

_pm.iconStream = getResources().openRawResource(+ R.drawable.pdf_icon);

stefan
  • 1,336
  • 3
  • 21
  • 46
1

I am just trying to test and I take a picture and send it to Parse

Put your test image somewhere either in res/raw/ (then, use your existing code, with R.raw.whatever_you_call_the_image) or in assets/ (then, use getAssets().open() with a relative path within assets/ to your image).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I tried to add `assets/` directory to the `res` folder than I put `getAssets().open("my_btn")` and I still get the same message – user1611830 Jul 24 '14 at 12:11
  • 1
    @user1611830: You forgot the file extension. With assets, you use the full filename with the extension. – CommonsWare Jul 24 '14 at 12:11
  • sorry I meant I put the right extension. By the way , when I put `Log.d("TAG", Arrays.toString(getAssets().list(".")));` I get an empty array – user1611830 Jul 24 '14 at 12:12
  • @user1611830: Since you have no files named `.`, that is not surprising. This is not a directory; it is entries in a ZIP-style archive (the APK). – CommonsWare Jul 24 '14 at 12:13
  • 1
    @user1611830 Its not to the res folder that you have to add the assets directory. First of all you need to add it in src>main(Since you are using android studio). And secondly don't forget the extension. – Harikrishnan Jul 24 '14 at 12:13
  • @user1611830: Yes, Harikrishan is correct -- I missed that in your original comment. `assets/` is a peer to `res/`, not a child of `res/`. – CommonsWare Jul 24 '14 at 12:14
  • ok so I moved the `assets` folder inside `src/main` and I put `is = getAssets().open("btn_fb.png");` (`is` is declared as `InputStream is`) but I still get the same message – user1611830 Jul 24 '14 at 12:18
  • @user1611830: Here is a sample project that demonstrates reading a file from `assets/` (in this case, to copy it to a local file, to then be served by `FileProvider`): https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/V4FileProvider – CommonsWare Jul 24 '14 at 12:27
  • sorry I don't want to bother, I have just updated my post. I followed exactly what you said, I rebuild the project but it still doesn't work. Is there a configuration I missed when adding the assets folder ? – user1611830 Jul 24 '14 at 12:45
  • @user1611830: From what I can see, that should be OK. Compare what you have with the sample project's code for accessing the asset, for the sample that I linked to in my previous comment. – CommonsWare Jul 24 '14 at 12:51
  • Ah ok, I am completely sorry, this error was thrown from another resource missing - sorry again – user1611830 Jul 24 '14 at 13:00
  • Is it the answer actual, will approach in http://stackoverflow.com/a/36062748/241986 work? – Boris Treukhov Aug 20 '16 at 13:17
  • @BorisTreukhov: I have no idea if it works. I would not use it. – CommonsWare Aug 20 '16 at 13:35