Skip to content

Commit

Permalink
Fix issues in #13
Browse files Browse the repository at this point in the history
  • Loading branch information
HBiSoft committed Jan 20, 2020
1 parent d9c75c5 commit f16be7f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
41 changes: 27 additions & 14 deletions pickit/src/main/java/com/hbisoft/pickit/DownloadAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.AsyncTask;
import android.provider.OpenableColumns;
import android.util.Log;
import android.webkit.MimeTypeMap;

import java.io.BufferedInputStream;
import java.io.File;
Expand All @@ -22,17 +21,14 @@ class DownloadAsyncTask extends AsyncTask<Uri, Integer, String> {
private WeakReference<Context> mContext;
private String pathPlusName;
private File folder;
private String filename;
private Cursor returnCursor;
private InputStream is = null;
private String extension;
private String errorReason = "";

DownloadAsyncTask(Uri uri, Context context, CallBackTask callback, String filename) {
DownloadAsyncTask(Uri uri, Context context, CallBackTask callback) {
this.mUri = uri;
mContext = new WeakReference<>(context);
this.callback = callback;
this.filename = filename;
}

@Override
Expand All @@ -42,8 +38,6 @@ protected void onPreExecute() {
if (context != null) {
folder = context.getExternalFilesDir("Temp");
returnCursor = context.getContentResolver().query(mUri, null, null, null, null);
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(mUri));
try {
is = context.getContentResolver().openInputStream(mUri);
} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -82,13 +76,8 @@ protected String doInBackground(Uri... params) {
returnCursor.close();
}

if (extension == null){
pathPlusName = folder + "/" + filename;
file = new File(folder + "/" + filename);
}else {
pathPlusName = folder + "/" + filename + "." + extension;
file = new File(folder + "/" + filename + "." + extension);
}
pathPlusName = folder + "/" + getFileName(mUri, mContext.get());
file = new File(folder + "/" + getFileName(mUri, mContext.get()));

BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(file);
Expand Down Expand Up @@ -118,6 +107,30 @@ protected String doInBackground(Uri... params) {

}

private String getFileName(Uri uri, Context context) {
String result = null;
if (uri.getScheme() != null) {
if (uri.getScheme().equals("content")) {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
if (cursor != null) {
cursor.close();
}
}
}
if (result == null) {
result = uri.getPath();
assert result != null;
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}

protected void onPostExecute(String result) {
if(result == null){
callback.PickiTonPostExecute(pathPlusName, true, false, errorReason);
Expand Down
26 changes: 10 additions & 16 deletions pickit/src/main/java/com/hbisoft/pickit/PickiT.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void getPath(Uri uri, int APILevel){
// Drive file was selected
if (isOneDrive(uri)||isDropBox(uri)||isGoogleDrive(uri)){
isDriveFile = true;
downloadFile(uri, "tempFile");
downloadFile(uri);
}
// Local file was selected
else {
Expand Down Expand Up @@ -57,7 +57,12 @@ public void getPath(Uri uri, int APILevel){
if (Utils.errorReason() != null && Utils.errorReason().equals("dataReturnedNull")) {
isFromUnknownProvider = true;
//Copy the file to the temporary folder
downloadFile(uri, getFileName(uri));
downloadFile(uri);
return;
}else if (Utils.errorReason() != null && Utils.errorReason().contains("column '_data' does not exist")){
isFromUnknownProvider = true;
//Copy the file to the temporary folder
downloadFile(uri);
return;
}
}
Expand All @@ -77,9 +82,8 @@ public void getPath(Uri uri, int APILevel){
//Todo: Add checks for unknown file extensions

if (!subStringExtension.equals(extensionFromMime) && uri.getScheme() != null && uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
String fileName = returnedPath.substring(returnedPath.lastIndexOf("/") + 1);
isFromUnknownProvider = true;
downloadFile(uri, fileName);
downloadFile(uri);
return;
}

Expand All @@ -95,19 +99,9 @@ public void getPath(Uri uri, int APILevel){

}

//Get the file name
private String getFileName(Uri uri) {
String replaced = String.valueOf(uri).replace("%2F", "/").replace("%20", " ").replace("%3A","/");
String name = replaced.substring(replaced.lastIndexOf("/") + 1);
if (name.indexOf(".") > 0) {
name = name.substring(0, name.lastIndexOf("."));
}
return name;
}

// Create a new file from the Uri that was selected
private void downloadFile(Uri uri, String fileName){
asyntask = new DownloadAsyncTask(uri, context, this, fileName);
private void downloadFile(Uri uri){
asyntask = new DownloadAsyncTask(uri, context, this);
asyntask.execute();
}

Expand Down

0 comments on commit f16be7f

Please sign in to comment.