From baece5b2c11d7103b39819bf4d70620ca36fae20 Mon Sep 17 00:00:00 2001 From: aiman-al-masoud Date: Wed, 11 Aug 2021 22:09:12 +0200 Subject: [PATCH] De-duplicated getFileFromUri() method, placed it in FileIO. --- .../noadpadlight/model/services/FileIO.java | 46 ++++++++++++++++++- .../noadpadlight/ui/BackupFragment.java | 40 +--------------- .../noadpadlight/ui/ImportFileFragment.java | 44 ++---------------- 3 files changed, 50 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/com/luxlunaris/noadpadlight/model/services/FileIO.java b/app/src/main/java/com/luxlunaris/noadpadlight/model/services/FileIO.java index 9aeea68..a09e441 100644 --- a/app/src/main/java/com/luxlunaris/noadpadlight/model/services/FileIO.java +++ b/app/src/main/java/com/luxlunaris/noadpadlight/model/services/FileIO.java @@ -1,6 +1,9 @@ package com.luxlunaris.noadpadlight.model.services; +import android.content.Context; +import android.net.Uri; + import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.exception.ZipException; @@ -10,9 +13,12 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.RandomAccessFile; public class FileIO { @@ -196,10 +202,46 @@ public static void copyFilesToDirectory(File[] files, String destDirPath){ } } + /** + * Given the uri of an external file, make a copy of it + * in app-internal storage and return it. + * @param contentUri + * @return + */ + public static File getFileFromUri(Context context, Uri contentUri) { + //Use content Resolver to get the input stream that it holds the data and copy that in a temp file of your app file directory for your references + File selectedFile = new File(context.getFilesDir(), "import"); //your app file dir or cache dir you can use + + try { + + InputStream in = context.getContentResolver().openInputStream(contentUri); + OutputStream out = new FileOutputStream(selectedFile); + + byte[] buf = new byte[1024]; + int len; + + if (in != null) { + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + out.close(); + in.close(); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return selectedFile; + } + + + + + + - - } diff --git a/app/src/main/java/com/luxlunaris/noadpadlight/ui/BackupFragment.java b/app/src/main/java/com/luxlunaris/noadpadlight/ui/BackupFragment.java index 447b440..29c6b66 100644 --- a/app/src/main/java/com/luxlunaris/noadpadlight/ui/BackupFragment.java +++ b/app/src/main/java/com/luxlunaris/noadpadlight/ui/BackupFragment.java @@ -14,12 +14,9 @@ import com.luxlunaris.noadpadlight.R; import com.luxlunaris.noadpadlight.control.classes.Notebook; -import com.luxlunaris.noadpadlight.control.classes.Paths; +import com.luxlunaris.noadpadlight.model.services.FileIO; import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; /** * Calls SAF (Storage Access Framework) and sharing API @@ -128,48 +125,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { case IMPORT_CODE: Uri uri = data.getData(); - File file = getFileFromUri(uri); + File file = FileIO.getFileFromUri(getContext(), uri); Notebook.getInstance().importPages(file.getPath()); break; - } } - /** - * Given the uri of an external file, make a copy of it - * in app-internal storage and return it. - * @param contentUri - * @return - */ - private File getFileFromUri(Uri contentUri) { - //Use content Resolver to get the input stream that it holds the data and copy that in a temp file of your app file directory for your references - File selectedFile = new File(getActivity().getFilesDir(), "import.zip"); //your app file dir or cache dir you can use - - try { - - InputStream in = getActivity().getContentResolver().openInputStream(contentUri); - OutputStream out = new FileOutputStream(selectedFile); - - byte[] buf = new byte[1024]; - int len; - - if (in != null) { - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - out.close(); - in.close(); - } - - } catch (Exception e) { - e.printStackTrace(); - } - - return selectedFile; - } diff --git a/app/src/main/java/com/luxlunaris/noadpadlight/ui/ImportFileFragment.java b/app/src/main/java/com/luxlunaris/noadpadlight/ui/ImportFileFragment.java index ecce90c..41d50cb 100644 --- a/app/src/main/java/com/luxlunaris/noadpadlight/ui/ImportFileFragment.java +++ b/app/src/main/java/com/luxlunaris/noadpadlight/ui/ImportFileFragment.java @@ -4,24 +4,20 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; - -import androidx.fragment.app.DialogFragment; - import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; +import androidx.fragment.app.DialogFragment; + import com.luxlunaris.noadpadlight.R; import com.luxlunaris.noadpadlight.control.classes.SETTINGS_TAGS; import com.luxlunaris.noadpadlight.control.classes.Settings; +import com.luxlunaris.noadpadlight.model.services.FileIO; import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; - public class ImportFileFragment extends DialogFragment { @@ -148,7 +144,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { case IMPORT_CODE: Uri uri = data.getData(); - selectedFile = getFileFromUri(uri); + selectedFile = FileIO.getFileFromUri(getContext(), uri); fileSelectedText.setText(getString(R.string.selected_word)+uri.getPath()); break; @@ -156,38 +152,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } - /** - * Given the uri of an external file, make a copy of it - * in app-internal storage and return it. - * @param contentUri - * @return - */ - private File getFileFromUri(Uri contentUri) { - //Use content Resolver to get the input stream that it holds the data and copy that in a temp file of your app file directory for your references - File selectedFile = new File(getActivity().getFilesDir(), "import"); //your app file dir or cache dir you can use - - try { - - InputStream in = getActivity().getContentResolver().openInputStream(contentUri); - OutputStream out = new FileOutputStream(selectedFile); - - byte[] buf = new byte[1024]; - int len; - - if (in != null) { - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - out.close(); - in.close(); - } - - } catch (Exception e) { - e.printStackTrace(); - } - - return selectedFile; - } /** * Set a FileRequester listener to whom to pass