Install .APK from Android Cache - android

I have problems installing an APK saved in Android internal Cache.
There are no issues saving the file in External Storage or on External Cache using context.getExternalCacheDir().
But if I try to use context.getCacheDir(), the log returns
/data/data/com.my.package/cache/update.apk: open failed: EACCES (Permission denied)
File file = context.getCacheDir();
File outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
//SAVE IN CACHE
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
It looks like internal cache doesn't allow the APK to be correctly read.
The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.
What can it be don to save the file in internal Cache?
Thanks

It looks like internal cache doesn't allow the APK to be correctly read.
That is because the installer app has no rights to read your file.
The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.
Then do not install it on the user's device. Any user can copy any APK off their device at any time after installation, so the only way to prevent the user from accessing the APK is to not have it on the device in the first place.
What can it be don to save the file in internal Cache?
Probably nothing. If you switch to openFileOutput(), you can see if MODE_WORLD_READABLE will be sufficient for the installer to proceed. Again, this will not stop the user from being able to access the APK file.

Try to look chmod command to get read\write permissions on internal folder... As for linux it looks like...
chmod 777 /data/data/*** or chmod 644 /data/data/***

Related

(Android)saving apk files in internal storage and install trying, get an parsing error

I'm trying to get apk file from usb storage and save internal storage in my app.
I saved apk file by using:
FileOutputStream fout = _context.openFileOutput(filePath, Context.MODE_PRIVATE);
And then I tried to install apk with:
private void installApk(String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)),"application/vnd.android.package-archive");
startActivity(intent);
}
But I got an error.
There is a Problem Parsing the Package
I tried to use:
FileOutputStream fout = _context.openFileOutput(filePath, Context.MODE_WORLD_READABLE);
And It worked well.
Why MODE_PRIVATE cause paring error?
if where you saved apk is path to internal data space of your application then MODE_PRIVATE flag will be prevent any other application can access your apk file (only you can access it).

How do I Install external APKs necessary for my application in Android Studio?

I have downloaded CSipSimple . Now for video call of this , I need to install CSipSimple-Codec-Pack and CSipSimple-Video-plugin apks . I need to install these two external apks with my Android application . These apks are necessary for installation of my application .
How can I install these apks with my Android application by programming ?
there are 2 ways you could try this
Since the code of your dependent apps are open source, get the apks put them in your application's assets directory, then when your app is run copy the apks of the other apps to the external storage and initiate install
if both those apps are using gradle build
then you can git clone the source of the apps, build them, then import them as modules to your application, that way when your app is installed these other apps are also installed along with yours
The 1st one only works when phones have sdcards, the 2nd one is simpler its more straight forward
There is no code for this
I assume you know how to get the source code of the applications you need right?
just copy paste them into your application's root folder, then in android studio right click your project mouse over new-> click on module -> import gradle project -> then select the required application.
For the 1st method take a look at this thread
Code Snippet:
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("myapk.apk");
out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/myapk.apk");
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/myapk.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} catch(Exception e) { }
The above code copies apk from your assests folder to sdcard, then installs it on the phone.

How do I grant super user permission to fileinputstream object and read all system files on my rooted device

I have a rooted device I am trying to read files from a specific folder /sdcard/videos using FileInputStream and successfully created a CHECKSUM value for that folder, Now I want to read all the files from my system folder and create a checksum value for it but when I pass the folder path which is /system I am unable to read few files and get the following error:
java.io.FileNotFoundException: system/bin/run-as: open failed: EACCES (Permission denied)
How do I overcome this, how do I grant super user permission or root access to read all the system related files ?
Simplified: Programatically Read file from sdcard/system folder using fileinputstream on rooted device
Havent tried it yet
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("md5sum filepath");
/* executes the md5sum binary command,replace with installation path of md5sum after you install busybox */
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
Create checksum by calling the md5sum binary
ie. If you have BusyBox installed on your device

Android: File Location

I am writing to a path mnt/sdcard/foldername. I get no errors and the file is written, but when I browse the internal storage of my phone I can not find the folder. Where would the folder be located? I have a galaxy nexus?
Code is here:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/statReporter/");
directory.mkdirs();
Log.d("Tag", directory.toString());
//Path and name of XML file
File file = new File(directory, appendTimeStamp("phoneNum") + ".csv");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//Write to CSV File
osw.write(message);
osw.write(',');
//Flush and close OutPutStreamWriter and close FileOutputStream
osw.flush();
osw.close();
fOut.close();
Log.d("File Logger:", "File write successfully!");
Cant find it in Windows Computer\Galaxy Nexus\Internal Storage but all other folders appear.
I used an app called OI File Managaer and I can view the folder and file on phone but how do I view it through Windows OS?
If you really want to find files on your device, I'd recommend one of the Google Play apps you can find (I personally like ASTRO File Manager) or, from the PC you can use (for instance) Android Commander (which, incidentally, will let you use the same file path structure you'll be using from within your developing environment).
I believe that Android devices will not actually show you all paths and available files when you browse it, for instance, with Windows Explorer.
If you're using Eclipse, in the DDMS perspective you can browse your device's file structure. On your right you have the "File Explorer" and as far as I remember, it's a pretty complete tool.
Don't use /mnt/sdcard for accessing external storage. You can use Environment.getExternalStorageDirectory() to access path to external storage. This may vary from device to device.

Acess files on root in device

I'm developing an application for Galaxy Tab, and now I need get some files on the /data/data// folder.
The emulator is really, really slow. So I'm testing on device, and need get the files from device (from emulator i'm able to do). How can I do that?
Add a debug function in your code to copy the required files from the protected folders onto your phone's SD card. You can then access them from your PC.
public static void backupFile() throws IOException {
String inFileName = "/data/data/your.package.name/........"; //TODO Use folder/filename
File inFile = new File(inFileName);
FileInputStream fis = new FileInputStream(inFile);
String outFileName = Environment.getExternalStorageDirectory()+"/........"; //TODO Use output filename
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0)
output.write(buffer, 0, length);
output.flush();
output.close();
fis.close();
}
The /data directory on a device has restricted permissions. To copy files off using adb you will need to have rooted your device.
adb pull <path>
will download files from the device to your current directory.
You can also use adb shell to explore your device using common shell commands. adb shell ls <path> will let you do this without actually entering the shell completely.
Edit - this will not work for files in the /data directory or other restricted directories unless you have root access to the device.

Categories

Resources