I tried executing the following line to load gif image, programatically.
gifInputStream = context.getResources().openRawResource(R.drawable.image);
But, studio used to indicate an error near "R.drawable.image".
I found a solution, or rather hack around to my issue. I just added "+" symbol. So, the line looks like this now...
gifInputStream = context.getResources().openRawResource(+R.drawable.image);
AndroidStudio doesn't show any error now.
it required raw file not drawable.
Please create res/raw/ directory and move your file in the raw directory.
after then:-
context.getResources().openRawResource(R.raw.yourfilename)
Related
I have data.xlsx file in Assets folder and need to get its path to apply some library.
Found several solutions here but neither works for me. For example -
File Not Found Exception In Xamarin program
I tried to use file:///android_asset/data.xlsx but here is no file too.
I checked the path with: System.IO.File.Exists(filePath);
So how to get path to this file? Maybe I have to place it in different folder?
Update: possibly I can solve it this way: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
But I can't find where to place it.
Update: this question is about Java and this is unclear how to use this solution in Xamarin.Android: How to get the android Path string to a file on Assets folder?
Looks like the file isn't included in the APK. Ensure that the Build Action of the file in the Asset-folder is set to AndroidAsset.
in general using Xamarin.android , you can use this code :
private string _fileName {get;set;}
public string FileFullPath
{
get
{
return Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures),_fileName);
}
}
Hope it helps.. :)
Looks like it's impossible.
Closest solution is to write file to Cache.
I've tried all solutions. Finally decided to post this question.
The situation is as follows:
My drawable has test.png image which I use in my project.
In eclipse it works perfectly but in Android Studio it doesn't work.
Also I'm not getting any compiler error.
I am getting only this type of error almost 54 times when cradle build. As this image is set of as background to 54 screens. :(
What could be the problem?
You should remove the extension part.
For example if your image is called mimg.png in your
resource files you should access it via #drawable/mimg.
Edit
If in eclipse is fine, then create the project new and add file by file. I also had this issues and after some tries ai gave up and created new and after added file by file.
As an alternative please check if you add it in other drawables folders.
That's it.
You need to reference your drawable without the extension: Like this:
android:src="#drawable/test"
Remove the image extension from your xml file and try that please!
I am trying to read an image in my C++ code
LOGD("Loading image '%s' ...\n", (*inFile).c_str());;
Mat img = imread(*inFile, CV_LOAD_IMAGE_GRAYSCALE);
CV_Assert(img.data != 0);
and get the following output:
09-25 17:08:24.798: D/IRISREC(12120): Loading image '/data/data/com.example.irisrec/files/input/osoba1.jpg' ...
09-25 17:08:24.798: E/cv::error()(12120): OpenCV Error: Assertion failed (img.data != 0) in int wahet_main(int, char**), file jni/wahet.cpp, line 4208
The file exists. But strange is, that if I try to preview the image using Root File Browser it is just black. I copied the files there manually.
EDIT:
The code works fine under Windows with .png and .jpg format. I am just trying to port an existing C++ project for Iris Recognition to Android.
imread() determines the type of file based on its content not by the file extension. If the header of the file is corrupted, it makes sense that the method fails.
Here are a few things you could try:
Copy those images back to the computer and see if they can be opened by other apps. There's a chance that they are corrupted in the device;
Make sure there is a file at that location and that your user has permission to read it;
Test with types of images (jpg, png, tiff, bmp, ...);
For testing purposes it's always better to be more direct. Get rid of inFile:
Example:
Mat img = imread("/data/data/com.example.irisrec/files/input/osoba1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data) {
// Print error message and quit
}
When debugging, first try to get more data on the problem.
It's an unfortunate design that imread() doesn't provide any error info. The docs just say that it'll fail "because of missing file, improper permissions, unsupported or invalid format".
Use the debugger to step into the code if you can. Can you tell where it fails?
Search for known problems, stackoverflow.com/search?q=imread, e.g. imread not working in OpenCV.
Then generate as many hypotheses as you can. For each one, think of a way to test it. E.g.
The image file is malformed (as #karlphillip offered). -- See if other software can open the file.
The image file is not a supported format. -- Verify the file format on your desktop. Test that desktop OpenCV can read it. Check the docs to verify the image formats that AndroidCV can read.
The image file is not at the expected path. -- Write code to test if there's a file at that path, and verify its length.
The image file does not have read permission. -- Write code to open the file for reading.
A problem with the imread() arguments. -- Try defaulting the second argument.
I was able to solve this issue only by copying the image files in code.I stored them in my asset folder first and copied them to internal storage following this example.
If someone can explain this to me please do this.
It could be a permission issue.You would have to request the permission from Java code in your Activity class like this in Android 6.0 or above. Also make sure that in your AndroidManifest.xml, you have the the following line :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your activity file add this:
if (PermissionUtils.requestPermission(
this,
HOME_SCREEN_ACTIVITY,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
Mat image = Imgcodecs.imread(filePath,Imgcodecs.IMREAD_COLOR);
}
I struggled a long time to find this and I was getting Mat object null for all the time before.
I need to get a file descriptor for a dae file, so I can get the Start Offset and Length. I have got file descriptors for png files, which work fine. I did this by using this code.
int id = 0;
id = getResources().getIdentifier("duck", "drawable", getPackageName());
mAssetDescriptor = getResources().openRawResourceFd(id);
Like I said, for my png's this worked fine, for my dae's, however, this did not work, I got the following exception:
android.content.res.Resources$NotFoundException:
File res/drawable/duck.dae from
drawable resource ID #0x7f020030
mAssetDescriptor was null.
So I tried to do another way. I put my dae files in the assets folder
mAssetDescriptor = getAssets().openFd("duck.dae");
This came up with the error
This file can not be opened as a file
descriptor; it is probably compressed.
What I would like to know, is how I can get the Start Offset and Length of the file without using the getFileDescriptor() and getStartOffset() functions, as it seems I can not use these.
I guess it is also worth nothing the dae files are all under 1mb, with duck.dae which i'm trying to load at the moment at 500k.
Thanks alot for the help
Tom
This probably works for pngs because they are not additionally compressed, while most other files are. See this thread: http://groups.google.com/group/android-ndk/browse_thread/thread/8274a4c51cc9cc1d
Try adding and extension to your files, that prevents file from being compressed.
I am trying to draw one sprite from atlas.. I created with Zwoptex the atlas and the plist file.
put the two files in the assest folder..
In the code I create new GameScene class
and try to load it..
//Return the ShareFrameCache object.
CCSpriteFrameCache frameCache = CCSpriteFrameCache.sharedSpriteFrameCache();
//Loading the list of frames from the list file.
frameCache.addSpriteFrames("level1.plist");
//Testing and see if I can load one frame to a sprite
CCSprite sprite = CCSprite.sprite("Screen_01_0029_BG_01-0.png");
//Set the position of the frame to the middle of the screen
sprite.setPosition(CGPoint.ccp(winSize.width/2,winSize.height/2));
//add the sprite as child so it can be seen on the phone.
addChild(sprite,0);
In the debugger I am getting these errors:
04-28 12:45:31.662: WARN/System.err(1147): java.io.FileNotFoundException: level1.png
04-28 12:45:32.642: WARN/System.err(1147): java.io.FileNotFoundException: Screen_01_0029_BG_01-0.png
04-28 12:45:32.622: ERROR/CCSpriteFrameCache(1147): Unsupported Zwoptex plist file format.
Screen_01_0029_BG_01-0.png refers to one of the frames in the level1.png atlas...
Thoughts ?
ER
first of all you need to pass the "true" statement as second parameter in the "CCSprite.sprite" constructor. This way you instruct cocos2d that you intent to use a sprite image which is part of a resource plist file instead as a stand alone image in the \res folder of your project. If that won't help you may need to remove the cocos2d JAR file from the \libs directory and download the source code from github. If you do this please comment out the "draw" methods at the CCMenuItemSpite.java class since it produces double images. The default behavior of the class works just fine. You may also need to remove some "#Override" directives, especially if you have a latest version of Eclipse as I do before you get the source files to be properly built and linked with your project. I hope that helps a bit...