Skip to content

Commit

Permalink
De-duplicated getFileFromUri() method, placed it in FileIO.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman-al-masoud committed Aug 11, 2021
1 parent 0f50aea commit baece5b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}












}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -148,46 +144,14 @@ 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;

}

}

/**
* 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
Expand Down

0 comments on commit baece5b

Please sign in to comment.