Just like the title says, i can't seem to get the images in my assets folder to open up.
public static final String CONTENT_URI = "file:///android:asset/";
Uri tmp = Uri.parse(CONTENT_URI + s);
holder.image.setImageURI(tmp);
I have tried a bunch of different syntax's, and I can't seem to get the asset to be detected. Keep getting resolveUri errors about it not finding the files.
s is a string such as hears.png which is indeed in the assets folder.
This is what you need to get images from asset
yourImageView.setImageBitmap(BitmapFactory.decodeStream(activity.getResources().getAssets().open("whateverResorce.png")));
Regards!
Related
I want to copy a single json file from Unity's Assets/Resources/ directory to the internal persistent memory of Android device (INTERNAL STORAGE/Android/data/<packagename>/files/) only once at the start of the game. I looked up all over the internet, with solutions saying to use Streaming assets and UnityWebRequests or WWW class which is obsolete now. I tried everything for hours now. And did not find a exact solutions, but only got null references and directory not found.
I added my json file to the <UnityProject>/Assets/Resources/StreamingAssets/, but it doesn't seem to detect any streaming assets. I understand an apk is a zip file and so I can only read the contents of streaming assets, but my I can't find a solution to this.
Once I get my json from streaming asset, I can finally add it to Application.persistentDataPath.
UPDATE
I had actually figured it out,
public void LoadFromResources() {
{mobilePath = "StreamingAssets/"; mobilePath = mobilePath + "stickerJson" + ".json";
string newPath = mobilePath.Replace(".json", "");
TextAsset ta = Resources.Load<TextAsset>(newPath);
string json = ta.text; Debug.Log(json); System.IO.File.WriteAllText(Application.persistentDataPath + "/stickers.json", json);
}
}
To copy a file you only need to have the information of the file and then create a new one in another place.
So if you got your json file into your Resources directory you can retreive like the documentation say:
//Load text from a JSON file (Assets/Resources/Text/jsonFile01.json)
var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
Then you only have to create your android file in your persistentDataPath as #Iggy says in the comments doing:
System.IO.File.WriteAllText(Path.Combine(Application.persistentDataPath, "file.txt"), jsonTextFile);
Note: Not really sure, but for other cases I think that StreamingAssets folder should be inside Assets, no inside Resources.
I need to download some resources from the net, then locally process them and show them on a BrowserActivity.
Since the resources (images) could be really big I'm saving them in the SDCard.
How may I refer to those stored resources from an html page? The html page is embedded into my app (It's in assets folder). I think this will fire some kind of security issues.
Is this the right approach? or is there any better solution?.
Thanks
Answer for question 1): You can refer to file on your sd card using "file://" prefix. See example below:
String fileName = "x"; // here is the file name of the downloaded image
String imagePath = "file://" + getApplicationContext().getFileStreamPath(fileName.getAbsolutePath();
String html = html+"<img src=\"" + imagePath + "\"/>" // embed that code in your modified html source.
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];
I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.
I need the name (String) of all files in res/raw/
I tried:
File f = new File("/");
String[] someFiles = f.list();
It looks like the root directory is the root of the android emulator...and not my computers root directory. That makes enough sense, but doesn't really help me find out where the raw folder exists.
Update: Thanks for all the great replies. It appears that some of these are working, but only getting me half way. Perhaps a more detailed description will aid
I want to get all the mp3 files in the raw folder so I can get all the names, then add them to a URI to play a random MP3 in the following way...
String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis";
Uri uri = Uri.parse(uriStr);
singletonMediaPlayer = MediaPlayer.create(c, uri);
When I put "dennis.mp3" into the assets folder, it does show up as expected, however, using the above code, I can't access that MP3 anymore, unless there is something along the line of:
String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis";
Uri uri = Uri.parse(uriStr);
To list all the names of your raw assets, which are basically the filenames with the extensions stripped off, you can do this:
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Since the actual files aren't just sitting on the filesystem once they're on the phone, the name is irrelevant, and you'll need to refer to them by the integer assigned to that resource name. In the above example, you could get this integer thus:
int resourceID=fields[count].getInt(fields[count]);
This is the same int which you'd get by referring to R.raw.whateveryounamedtheresource
This code will retrieve all the files from 'New Foder' of sdCard.
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "New Folder");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
Log.i("file names", name);
}
}
and also make sure to add android sd card write permission in your manifest.xml file
I need the name (String) of all files in res/raw/
There are no files in res/raw/ on the device. Those are resources. There is no good way to iterate over resources, other than by using reflection to iterate over the static data members of the R.raw class to get the various ID names and values.
but doesn't really help me find out where the raw folder exists.
As a folder, it only exists on your development machine. It is not a folder on the device.
You can use AssetManager:
As far as I can remember, you will have list with (just try different paths):
final String[] allFilesInPackage = getContext().getResources().getAssets().list("");
Look at http://developer.android.com/reference/android/content/res/Resources.html
You can generally acquire the Resources instance associated with your application with getResources().
Resources class provides access to http://developer.android.com/reference/android/content/res/AssetManager.html (see to getAssets() method).
And finally obtain access to your packaged (apk) files with AssetManager.list() method.
Enjoy!
From a Context, you can call something like this:
getResources().getAssets().list("thePath");
link to documentation