Reputation: 395
I know most of file-accessing methods in Android are in general performed by accessing into drawable(using R.drawable..) or, some, assets(using AssetManager).
Anyway, Can I access to any other folders than those above just like myfolderA or myNewFolderA under my android project name?
If possible, I guess those other ways might not be considered normal, but I just want to know if that is possible or not.
Upvotes: 0
Views: 1110
Reputation: 27003
Can I access to any other folders than those above just like
myfolderA
ormyNewFolderA
under my android project name?
Yes you can as explained in Android Documentation:
Android uses a file system that's similar to disk-based file systems on other platforms. You can work with the Android file system to read and write files with the File APIs
.
A
File
object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example, it's good for image files or anything exchanged over a network.
Also you must be carefull about where you read/write your files:
Internal and External Storage
All Android devices have two file storage areas: "internal" and "external" storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not. The following lists summarize the facts about each storage space.
Save a File on Internal Storage
When saving a file to internal storage, you can acquire the appropriate directory as a File
by calling one of two methods:
getFilesDir()
Returns a File representing an internal directory for your app.
getCacheDir()
Returns a File representing an internal directory for your app's temporary cache files. Be sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB. If the system begins running low on storage, it may delete your cache files without warning.
To create a new file in one of these directories, you can use the
File()
constructor, passing theFile
provided by one of the above methods that specifies your internal storage directory. For example:
File file = new File(context.getFilesDir(), filename);
Permissions For externa storage:
in order to allow you read and write you must give permissions:
<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Upvotes: 1
Reputation:
You can include external file resources from
res/raw
folder using
Resources myResources = getResources();
InputStream myFlie = myResources.openRawResources(R.raw.myFileName);
Upvotes: 0
Reputation: 911
Try getting a Bitmap and converting it to a Drawable:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Convert it like that:
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
If you just want to use images from a different path, this should work.
Upvotes: 0