ExtractionLib
Sometimes we need a lot of files to be downloaded from a server,we can download all of them in the form of jar and then extract your jar file which may carry images, excel files, etc, you don't have need to worry anymore, a lot of code is already there in library.
Just you have to configure a little code in your android activity.
+First add ExtractionLib in your app level build file as follows
dependencies {
compile 'com.github.ankitdubey021:ExtractionLib:2.0'
}
-In project level build file, add the jitpack repository
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
}
}
}
Now in your activity, call the Extract class method with following prototype
public class Extract extends ActivityCompat{
public static void extract(File jarfile, String path)throws Exception{ --- }
}
+Jar file represents the file to be extracted, and path represents where to be extracted
Sample code
void extract(){
try {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "value1.jar");
Extract.extract(outputFile, new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "extration").toString());
Toast.makeText(MainActivity.this, "Extracted!", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
System.out.println(e);
}
}
Make sure, you've asked for permission at runtime to read the file as follows, otherwise you'll get exception as FileNotFoundException
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
}
, 1);
}
@Override public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
extract();
}
else {
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
}
for any query, you can mail me at -> [email protected]
}