Opening document located in application directory by another activity - java

I have the following code which tries to open file with default reader:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// type can be "application/msword" or "application/pdf"
intent.setDataAndType(Uri.fromFile(file), type);
activity.startActivity(intent);
File is located on Android device within the application folder:
/data/data/air.com.foo.foo/com.foo.foo/Local Store/folder1/file.ppt
The problem is that launched activity fails to find the file. I checked for file presence in caller code so it is not the problem.
Furthermore, activity finds the file if I copy it from application folder to a folder shared by applications (on sdcard).
I don't understand why it fails since I have added:
intent.addFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION);
The problem is that copying file to a shared folder for me is not a satisfactory solution as I would like it to stay within my initial application folder.

In the activity that has access to the file, You probably want to do a
file.setReadable(true, false);
the true is for readability, the false is for "owner only".
Since you can find it, but not read it, it would seem that lacking this permission is the issue.

Related

Why is android unable of finding my file?

I want to access a file in included in the folder of the app and I want to be able to access it localy. I also don't want to use methods like android.getresources() and just use
File file = new File( filePath );
My file is saved in app/src/main/assets/ball.obj
The problem is when I do File file = new File("app/src/main/assets/ball.obj");, it alwas gives me a FileNotFoundException. I also tried moving the file in other places and absolute pathes, but it still doesn't work. Please help.
My file is saved in app/src/main/assets/ball.obj
That is a relative path to a file on your development machine. Assets are not files on the filesystem of the device. You cannot use File to access an asset.
To get an InputStream on your asset, call getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets().
For your call to open(), you need to pass in the relative path within assets/ to the asset that you wish to read in. So, if you are in a method of your Activity, getAssets().open("ball.obj") will give you an InputStream that you can use to read in the contents of that particular asset.

Getting a time of deleted folder \ file

I am a newbie. A wanna to check for existing a folder or file in directory in past.
For better. Example, i may a directory C:\Users\Admin\AppData\ and i wanna to check of existing a directory Test in that path. That maybe be checket by:
File file = new File(System.getenv("APPDATA") + "\\Test\\");
if(file.isDirectory()){
///...
} else { ////....}
But i wanna to check if that directory is deleted - when. Please help with code examples... be VERY and VERY thanks
Instead of the File class, I recommend looking at the Files class - it is there to help you do many things. For example, Files.createFile(...) will check to see if a file exists before creating. You can then pass a positive result to FileWriter(...) for your work.
You can check for the presence of a file of folder, but not that it was deleted (e.g. checking a log file of past actions). I recommend using the logic of "if not there then it never existed or was deleted". Another option when working with files is to use parameters to always overwrite the file if that is what you want.
You are asking a question about the operating system. What happens after a file or folder is deleted is unique to each operating system. A notional recycle bin's awareness of a file or folder's original location was, and where that content may have been moved to is specific to an operating system (and usually isn't just moved into another folder).

Install an android application through another application

I need to create an application that will install some apk (the name is myapp.apk) file that I created. (I checked myapp.apk alone and it works fine)
I've created a folder raw inside the res folder and put the myapp.apk file there.
I added the following code to my application:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("R.raw.myapp")), "application/vnd.android.package-archive");
But I'm getting a parse error on the emulator (eclipse android emulator).
I appreciate your help
First, R.raw.myapp is not a file. It is an integer identifier of a raw resource.
Second, you cannot install an APK from a resource (or an asset, or a ContentProvider). It must be a file on the filesystem, readable by other processes.
So, use getResources().openRawResource() to get an InputStream on your APK, write it to external storage, then use the file on external storage in your Intent.

Why fopen gets fail in native method due to permission issue from Android-NDK application?

see i have following code in one native call
errno = 0;
FILE *fp;
fp = fopen("jigar.txt","wb");
if(fp == NULL)
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "FOPEN FAIL with %d",errno);
else
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "FOPEN pass ");
which gets fail and shows
FOPEN FAIL with 30
now here 30 means it shows error
#define EROFS 30 /* Read-only file system */
In MainFest file on my application i have added this line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
still i am getting this error..
How to resolve this issue?
Edit
thanks by specifies folder name it works But
In my case i have one Library which has such fopen() call where file names i can not give. It takes it default filename.
And i am using this Library in my ndk application so in this case how to solve this?
By default when you are running your App, it runs in its own context. fopen("jigar.txt","wb"); will try to open the file in current directory, which i think mostly would be /data/.. So you cant create files like that. Instead, if you want a folder inside /data/ you can call a function gteDir() and inside it you can create your own files. Ok this is all for general information.
Coming to your problem, as mentioned above you need to give absolute path to create a file in a different directory. This is the same case even in Linux.
For the Library thing, you can do two things.
1) Make changes in the library source code and compile it again using NDK. While making changes, give some string as the argument for fopen() which you will pass it while executing the Application.
2) remount the filesystem in which your library is creating the file, then do chmod 777 to the specific directory inside which your file is being created. Now execute the Application. It should work. But this is not generic. If you are doing it for some testing purpose then this solution is simplest and time saving...
If stuck somewhere, let me know..
You should specify folder where to write the "jigar.txt" file. Like this:
fp = fopen("/sdcard/figar.txt", "wb");
yuppi finally i got that
i have did this way
first change the current directory of my process from "/" to "/sdcard/"
chdir("/sdcard/");
and then it find path with respect to /sdcard/
and everything WORKING..!!!

Android internal storage reading file

I have a problem with Android internal storage. I have created folder in package root folder calling getDir() and with MODE_WORLD_WRITEABLE because I want camera app to write captured image in this folder. Anyway, I can see that captured image is inside that folder with DDMS.
Problem is that I cannot read that file.
I tried to read file with this code:
File file = context.getDir("images", Context.MODE_WORLD_READABLE);
File image = new File(file, "image.jpeg");
if (image.canRead())
Log.w("read", "can read");
else
Log.w("read", "can't read");
And in LogCat there is only second message (can't read).
I have also tried to create FileInputStream with file name but I receive FileNotFoundException.
Can somebody tell me what I'm doing wrong here?
Thanks in advance
EDIT:
Reading file is correct but only problem is that when cam app is saving image to specified location, permission set by cam app to image file are -rwxrwx---. After changing permissions with
adb (chmod 777 image.jpeg)
I was able to read image. Interesting thing is that cam app is writing images files to sdcard with ----rwxr-x.
Is there any way to change file permission in runtime?
Why not put it in the default photo directory?
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
( From: http://developer.android.com/reference/android/os/Environment.html )
See also a more complete example invoking the camera app: http://developer.android.com/training/camera/photobasics.html
Referencing to Android developers you should use openFileOutput() and openFileInput() to work on the internal storage... you are not allowed / not able to make dirs there. Android does it itself.. Those data will be cleared when your application will be deleted.

Categories

Resources