adds FilePath utils class and removes previous commons dependency
This commit is contained in:
parent
811016acbc
commit
c02b5e18e7
Binary file not shown.
|
@ -25,7 +25,7 @@
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|
|
@ -34,6 +34,5 @@ android {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'in.gauriinfotech:commons:1.0.8'
|
implementation 'com.android.support:appcompat-v7:27.0.0'
|
||||||
//compileOnly files('tmplibs/flutter.jar')
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,156 @@
|
||||||
|
package com.mr.flutter.plugin.filepicker;
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.content.ContentUris;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.provider.DocumentsContract;
|
||||||
|
import android.provider.MediaStore;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Credits to NiRRaNjAN from package in.gauriinfotech.commons;.
|
||||||
|
**/
|
||||||
|
public class FilePath
|
||||||
|
{
|
||||||
|
|
||||||
|
private static final String tag = "FilePathPicker";
|
||||||
|
|
||||||
|
public static String getPath(final Uri uri, Context context)
|
||||||
|
{
|
||||||
|
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
||||||
|
if (isKitKat)
|
||||||
|
{
|
||||||
|
return getForApi19(context, uri);
|
||||||
|
} else if ("content".equalsIgnoreCase(uri.getScheme()))
|
||||||
|
{
|
||||||
|
if (isGooglePhotosUri(uri))
|
||||||
|
{
|
||||||
|
return uri.getLastPathSegment();
|
||||||
|
}
|
||||||
|
return getDataColumn(context, uri, null, null);
|
||||||
|
} else if ("file".equalsIgnoreCase(uri.getScheme()))
|
||||||
|
{
|
||||||
|
return uri.getPath();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(19)
|
||||||
|
private static String getForApi19(Context context, Uri uri)
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ API 19 URI :: " + uri);
|
||||||
|
if (DocumentsContract.isDocumentUri(context, uri))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Document URI");
|
||||||
|
if (isExternalStorageDocument(uri))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ External Document URI");
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
final String type = split[0];
|
||||||
|
if ("primary".equalsIgnoreCase(type))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Primary External Document URI");
|
||||||
|
return Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||||
|
}
|
||||||
|
} else if (isDownloadsDocument(uri))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Downloads External Document URI");
|
||||||
|
final String id = DocumentsContract.getDocumentId(uri);
|
||||||
|
final Uri contentUri = ContentUris.withAppendedId(
|
||||||
|
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
|
||||||
|
|
||||||
|
return getDataColumn(context, contentUri, null, null);
|
||||||
|
} else if (isMediaDocument(uri))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Media Document URI");
|
||||||
|
final String docId = DocumentsContract.getDocumentId(uri);
|
||||||
|
final String[] split = docId.split(":");
|
||||||
|
final String type = split[0];
|
||||||
|
|
||||||
|
Uri contentUri = null;
|
||||||
|
if ("image".equals(type))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Image Media Document URI");
|
||||||
|
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("video".equals(type))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Video Media Document URI");
|
||||||
|
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
} else if ("audio".equals(type))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ Audio Media Document URI");
|
||||||
|
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String selection = "_id=?";
|
||||||
|
final String[] selectionArgs = new String[]{
|
||||||
|
split[1]
|
||||||
|
};
|
||||||
|
|
||||||
|
return getDataColumn(context, contentUri, selection, selectionArgs);
|
||||||
|
}
|
||||||
|
} else if ("content".equalsIgnoreCase(uri.getScheme()))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ No DOCUMENT URI :: CONTENT ");
|
||||||
|
if (isGooglePhotosUri(uri))
|
||||||
|
return uri.getLastPathSegment();
|
||||||
|
|
||||||
|
return getDataColumn(context, uri, null, null);
|
||||||
|
} else if ("file".equalsIgnoreCase(uri.getScheme()))
|
||||||
|
{
|
||||||
|
Log.e(tag, "+++ No DOCUMENT URI :: FILE ");
|
||||||
|
return uri.getPath();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getDataColumn(Context context, Uri uri, String selection,
|
||||||
|
String[] selectionArgs)
|
||||||
|
{
|
||||||
|
Cursor cursor = null;
|
||||||
|
final String column = "_data";
|
||||||
|
final String[] projection = {
|
||||||
|
column
|
||||||
|
};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
|
||||||
|
null);
|
||||||
|
if (cursor != null && cursor.moveToFirst())
|
||||||
|
{
|
||||||
|
final int index = cursor.getColumnIndexOrThrow(column);
|
||||||
|
return cursor.getString(index);
|
||||||
|
}
|
||||||
|
} finally
|
||||||
|
{
|
||||||
|
if (cursor != null)
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isExternalStorageDocument(Uri uri)
|
||||||
|
{
|
||||||
|
return "com.android.externalstorage.documents".equals(uri.getAuthority());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDownloadsDocument(Uri uri)
|
||||||
|
{
|
||||||
|
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isMediaDocument(Uri uri)
|
||||||
|
{
|
||||||
|
return "com.android.providers.media.documents".equals(uri.getAuthority());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isGooglePhotosUri(Uri uri)
|
||||||
|
{
|
||||||
|
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,17 +5,16 @@ import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import in.gauriinfotech.commons.Commons;
|
|
||||||
import io.flutter.plugin.common.MethodCall;
|
import io.flutter.plugin.common.MethodCall;
|
||||||
import io.flutter.plugin.common.MethodChannel;
|
import io.flutter.plugin.common.MethodChannel;
|
||||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
||||||
|
@ -47,7 +46,7 @@ public class FilePickerPlugin implements MethodCallHandler {
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
Uri uri = data.getData();
|
Uri uri = data.getData();
|
||||||
Log.i(TAG, "URI:" +data.getData().toString());
|
Log.i(TAG, "URI:" +data.getData().toString());
|
||||||
String fullPath = Commons.getPath(uri, instance.context());
|
String fullPath = FilePath.getPath(uri, instance.context());
|
||||||
String cloudFile = null;
|
String cloudFile = null;
|
||||||
|
|
||||||
if(fullPath == null)
|
if(fullPath == null)
|
||||||
|
@ -115,9 +114,15 @@ public class FilePickerPlugin implements MethodCallHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startFileExplorer() {
|
private void startFileExplorer() {
|
||||||
|
Intent intent;
|
||||||
|
|
||||||
if (checkPermission()) {
|
if (checkPermission()) {
|
||||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
|
||||||
|
intent = new Intent(Intent.ACTION_PICK);
|
||||||
|
}else{
|
||||||
|
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
intent.setType("application/pdf");
|
intent.setType("application/pdf");
|
||||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
instance.activity().startActivityForResult(intent, REQUEST_CODE);
|
instance.activity().startActivityForResult(intent, REQUEST_CODE);
|
||||||
|
|
Loading…
Reference in New Issue