Install an android application through another application - java

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.

Related

Find a directory in Android Studio Project not in the phone

I am trying to get access to a text file (log.txt) in a directory. All other questions on this topic refer to getting directories from the emulators internal storage.
My file structure is as such
>androidApp
->App
-->Build
-->src
--->game_log
---->log.txt
--->Main
---->(Android app Code further)
Using new File(System.getProperty("user.dir) + "app\\src\\game_log\\log.txt").exists() gets me false.
Another thing I tried was System.getProperty("user.dir") but that yields me /.
Contextwrapper.getPath() gets me the path of the emulators storage.
Is the file structure of Android Studio different or I am using the wrong method to get the file from my project folder?

Getting name of original .APK file in Android

I am trying to figure out how to get the name of the original .apk file from which an application was installed. I know that you can retrieve the package name and the source directory of the installed application by using the ApplicationInfo class (described here: Android: how to get the name of apk file programmatically?) but this is NOT what I want.
As an example, suppose I have a simple application that is named TestApp (via the android:label tag in the AndroidManifest.xml file). Furthermore, suppose I generate an .apk file for the application above, rename said .apk file to TestApp_JohnDoe.apk, and then email that application to John Doe for installation. When John Doe installs the application by clicking on the .apk file I would like to be able to read the filename of the original .apk, TestApp_JohnDoe.apk, that I sent to him. Using the ApplicationInfo class, more specifically the sourceDir method gives me the install directory and application name, /data/app/TestApp.apk, which is not what I am looking for. I know that it is probably possible to scan all available directories looking for the original .apk file, but I am hoping to avoid this.
In summary, I would like to programatically retrieve the .apk filename/source directory of the installer .apk, such as /storage/sdcard0/Download/TestApp_JohnDoe.apk, as opposed to the .apk filename/source directory of the actual installed application, /data/app/TestApp.apk.
It is possible to let the app know the apk name that was set at compile time, if that is of any use to you
applicationVariants.all { variant ->
// Change the target apk file name and path
String apk_path = "$rootProject.projectDir/bin"
String apk_name = "john_doe.apk"
project.delete(project.apk_path) //Delete any remains
String apkFile = String.format("%s/%s", apk_path, apk_name)
variant.outputs[0].outputFile = new File(apkFile)
// This can be used in Java runtime by using getString(R.string.apk_name)
resValue "string", "apk_name", apk_name
}
But what you are asking, I am pretty sure it is not possible

Using DownloadManager to download into new folder on phones storage

Im using a download manager which downloads a file into a new folder on the phone storage. Here is the code im using:
DownloadManager.Request downloadSample = new DownloadManager.Request(Uri.parse(urlSample));
downloadSample.allowScanningByMediaScanner();
downloadSample.setDestinationInExternalPublicDir("/Samples/"+previewName, "sample.ttf");
downloadSample.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(downloadSample);
This works perfectly fine on many of the devices Ive tested on, but a few devices force close the app with the following error in the log:
E/AndroidRuntime(29918): java.lang.IllegalStateException: Unable to create directory: /storage/sdcard0/Samples/Helvetica
E/AndroidRuntime(29918): at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:507)
Its annoying that it works fine on some but not at all on other devices. Does anyone know why this is happening?
The documentation for setDestinationInExternalPublicDir(String dirType, String subPath) says that dirType can take only specified values.
Here dirType can only be the android defined directories for example:
Environment.DIRECTORY_DOWNLOADS
etc.
you can find all the possible values of dirType at: http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
So the proper way to use this method is:
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath)
Also note that this will make the folder in Downloads folder on phone storage. Download manager will not allow you to make a folder in the default directory of phone storage.
This will not give you IllegalStateException for creating directory.

Opening document located in application directory by another activity

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.

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