I have two files (for example) that I want to put in my apk main expansion file. Let these files be img1.jpg and img2.jpg. I compress them using the following command:
zip -n .jpg main.123.in.example.app.obb *.jpg
(The version code is 123 and the package is in.example.app). This creates an obb file: main.123.in.example.app.obb
I place this code on an appropriate path. I can access the files img1.jpg and img2.jpg easily by calling the following:
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + "/Android/obb/in.example.app");
String pathToObb = expPath + File.separator + "main.123.in.example.app.obb";
ZipResourceFile zip = new ZipResourceFile(pathToObb);
InputStream iStream = zip.getInputStream("img1.jpg");
Bitmap bmp = BitmapFactory.decodeStream(iStream, null, options);// options to decode
This works perfectly. Now, how does one read an encrypted zip file created using the following:
zip -n -encrypt .jpg main.123.in.example.app.obb *.jpg
Is there any way we can read such an encrypted zip file? Or do we resort to the jobb tool to do the encryption and use StorageManager to mount encrypted obb and read from there?
Related
I see the image file in DDMS file explorer as /mnt/shell/emulated/0/DCIM/myPicsFolder/, if I write
final String uploadFilePath = "/mnt/shell/emulated/0/DCIM/myPicsFolder/";
final String uploadFileName = "mypic.jpg";
String sourceFileUri = uploadFilePath + uploadFileName;
File sourceFile = new File(sourceFileUri);
if (sourceFile.isFile()) {/*Upload Image*/}
but the if statemente returns false :-/ , what's wrong? I'm testing code in Samsung Core 2 with Android 4.4. I've checked and rechecked if the path is correctly written and it's ok.
what's wrong?
Root paths in adb shell are not the same as root paths for your app. Never hardcode root paths in your app. Always use methods on Environment or Context to get at root paths.
In this case, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) will give you a File pointing to the DCIM directory that you are trying to work with. So, use something like:
File dcim=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File sourceFile=new File(new File(dcim, "myPicsFolder"), "mypic.jpg");
I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?
you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help
Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);
Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.
If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");
I have a problem.
I use this code to read the content of a zip file:
File file = new File(TogglesManager.EXTERNAL_STORAGE_THEMES_DIRECTORY+"filename.zip");
ZipFile zip = new ZipFile(file);
But when I try to use an assets I have a FileNotFoundException.
To read asset zip file I use this code:
File file = c.getFileStreamPath("assetsFile.zip");
ZipFile zip = new ZipFile(file);
The "file" is not null, because if I write file.getName(); I see the correct file name.
I don't want to use ZipInputStream class, but only ZipFile
You cannot access Assets using normal file operations. You will have to use
AssetManager assetManager = mContext.getAssets();
InputStream is = assetManager.open("assetsFile.zip");
But since ZipFile does not take InputStream as a parameter, you will have to copy asset file onto internal storage and then use File
Further AFAIK, apk is zipped, so storing a zipped file inside assets might not change storage used by much, so maybe you donot need to use zipfile inside assets.
i want to access the /data/local/tmp for any random access file
(RAF) file how to get the path of that file?
go this way and read the raf file line by line but be assure that the folder have read permission to other user or you can change it from adb shell by using chmod commmand .....
File finalFile = new File("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
For accessing the any file through code , you must know the path of the file.
and then you can use the code:
File finalFile = new File/t("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
"/data/local/tmp" is the path for the file. It can differ file to file. So you must know the path of file before accessing it.