Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a File instance of a drawable?

Below is the code that I cope with logo printing. The logo is placed in res/drawable folder. When I run the app, it throws:

java.io.FileNotFoundException: /android.resource:/com.android.test/2130837505 (No such file or directory).

Any advice?

    public  boolean printLogo()
    {
      Uri logo_path = Uri.parse("android.resource://com.android.test/" + R.drawable._logo);
      File logo = new File(logo_path.toString());
      byte[] logo_bytes = new byte[(int) logo.length()];
      System.out.print("Length:" + logo.length());
      FileInputStream fs;
      try {
          fs = new FileInputStream(logo);
          fs.read(logo_bytes);
          fs.close();
          mChatService.write(logo_bytes);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }catch (IOException e) {
          e.printStackTrace();
      }
      return true;
    }
like image 396
user1437534 Avatar asked Jun 25 '12 07:06

user1437534


People also ask

How do I get the URI of a drawable image?

You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().

How do I open a drawable file?

Inside Android Studio go to File -> Settings -> Plugins -> Browse repositories. Search Android Drawable Preview.

What folder is Drawables directory in?

In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project.

What is drawable XML file?

An XML file that defines a drawable that changes the size of another Drawable based on its current level value. Creates a ScaleDrawable. Shape Drawable. An XML file that defines a geometric shape, including colors and gradients. Creates a GradientDrawable .


1 Answers

yes you should add the resource of such type under assets or raw directory...

but if you have any limitation ans you only need byte array can try

Bitmap bmp= BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.icon_resource);

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
   byte[] byteArray = stream.toByteArray();
like image 149
Dheeresh Singh Avatar answered Oct 05 '22 02:10

Dheeresh Singh