I have ported my android application to BB10. In my application there is one feature where we can send email with image attachment. In android it works fine but in BB10 image is not attached to mail.
I implement custom content provider for android. So when I select any image it will give the path as "content://providername/mail/attachment/image name" but it doesn't work on BB10.
In the BB10 relative path like "content: //" is not working. BB10 only works on Absolute path.
Code sample:
Path of image: content://com.abc.provider.local.file/mail/attachment/1.jpg
Uri.parse(LocalFileProvider.MAIL_FILE_URI + picture.getFileName()
In above code
LocalFileProvider.MaIL_FILE_URL = content://com.aba.provider.local.file/mail/attachment
picture.getFileName() = 1.jpg
Now how to convert this path to Absolute path Example like : file://
BB10 has the bar-descriptor.xml file that contains info about your app. One of the settings is the location of assets. For example - if you want to include a directory called "assets" with your package - you would have the followinf xml entry in your ..bar.xml file
<asset path="assets">assets</asset>
This assumes that you have the folder called "assets" in the root of your project.
Now you can place additional folders and files under "assets" and you can get at it by using the following URI
asset:///graphics/myimage.png
This would be an image located under /assets/graphics/myimage.png
Hope this helps.
Related
Let's say I package an image with my app and I want to open it with the default image viewer/whatever image viewer the user has chosen to be the default. How would I do that?
There's already this post: Open an image using URI in Android's default gallery image viewer but many of the answers are obsolete because due to the introduction of android N, a content provider must be used.
The only answer I can find is this one:
File file = ...;
final Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
but, according to the author of this solution, this code only works for images stored externally, not ones that may be packaged with my app.
You won't be able to open an image packaged with the app (in drawable or whatever resources) through an external application. You should first copy it into (at least) an internal file storage. After that you can implement a FileProvider to provide access to this file.
Let me know if you need more details on this. Hope it helped.
I use some class. That's constructor needs some file path that contains some files.
ex)
Komoran komoran = new Komoran("D:\program_project\lib");
Now I'm making android app. so I can't use absolute path (Because other people who download my app don't have that folder and files)
so I decide to use 'assets' folder that is maybe in APK file. So, final code is like below.
Komoran komoran = new Komoran("file:///android_asset");
but It seems like folder path is wrong(this class doesn't work). What I did wrong ?
This "file:///android_asset" path is used for applications which uses webview ex. cordova/phonegap. As it is part of resources because when Apk is created you can not use this path to access your assets folder. You have to work with context.getResources().getAssets().open("fileName")this code only.
Maybe u can add this code:
context.getResources().getAssets().open("fileName").
u will get a inputstream, and u can do something u want.
No need to use getResources.
You can use directly
context.getAssets().open("fileName").
My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.
I have an Android app that displays a comic book. To make use of the built-in zoom controls, I am loading the pictures in a WebView like so:
webView.loadUrl("file:///android_asset/page1.jpg");
This is working just fine, however, since the images are in the assets folder, they are not being compressed which makes my .apk enormous. I was wondering how to reference resource files (from the res/drawable folder) with a file path like I did above with the assets. Does anyone know what that path would look like? I've tried things like "file:///res/drawable/pagetitle.jpg" with no success. Thanks for the help.
Update:
I found that "file:///android_res/drawable/page1.jpg" was the path that I was looking for.
from this site
Using the resource id, the format is:
"android.resource://[package]/[res id]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
or, using the resource subdirectory (type) and resource name (filename without extension), the format is:
"android.resource://[package]/[res type]/[res name]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
I also had to use loadDataWithBaseURL instead of just plain loadData to get the file:///android_res/drawable/page1.jpg to work.
I want to know what does file:/// mean while loading a html file from the assets folder in android
Is it an absolute path name which points to the root directory?
I saw this in the tutorial for phonegap by the way.
file:/// is a URI (Uniform Resource Identifier) that simply distinguishes from the standard URI that we all know of too well - http://.
It does imply an absolute path name pointing to the root directory in any environment, but in the context of Android, it's a convention to tell the Android run-time to say "Here, the directory www has a file called index.html located in the assets folder in the root of the project".
That is how assets are loaded at runtime, for example, a WebView widget would know exactly where to load the embedded resource file by specifying the file:/// URI.
Consider the code example:
WebView webViewer = (WebView) findViewById(R.id.webViewer);
webView.loadUrl("file:///android_asset/www/index.html");
A very easy mistake to make here is this, some would infer it to as file:///android_assets, notice the plural of assets in the URI and wonder why the embedded resource is not working!
If someone uses AndroidStudio make sure that the assets folder is placed in
app/src/main/assets
directory.
The URI "file:///android_asset/" points to YourProject/app/src/main/assets/.
Note: android_asset/ uses the singular (asset) and src/main/assets uses the plural (assets).
Suppose you have a file YourProject/app/src/main/assets/web_thing.html that you would like to display in a WebView. You can refer to it like this:
WebView webViewer = (WebView) findViewById(R.id.webViewer);
webView.loadUrl("file:///android_asset/web_thing.html");
The snippet above could be located in your Activity class, possibly in the onCreate method.
Here is a guide to the overall directory structure of an android project, that helped me figure out this answer.
It is actually called file:///android_asset/index.html
file:///android_assets/index.html will give you a build error.
It took me more than 4 hours to fix this problem. I followed the guide from http://docs.phonegap.com/en/2.1.0/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android
I'm using Android Studio (Eclipse with ADT could not work properly because of the build problem).
Solution that worked for me:
I put the /assets/www/index.html under app/src/main/assets directory. (take care AndroidStudio has different perspectives like Project or Android)
use super.loadUrl("file:///android_asset/www/index.html"); instead of super.loadUrl("file:///android_assets/www/index.html"); (no s)
it's file:///android_asset/... not file:///android_assets/... notice the plural of assets is wrong even if your file name is assets