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;
}
You should use ContentResolver to open resource URIs: Uri uri = Uri. parse("android. resource://your.package.here/drawable/image_name"); InputStream stream = getContentResolver().
Inside Android Studio go to File -> Settings -> Plugins -> Browse repositories. Search Android Drawable Preview.
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.
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 .
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With