Fix share from SwiftKey Keyboard and clear temp files (#1585)
* Fix share from SwiftKey Keyboard and clear temp files * feedback review
This commit is contained in:
parent
006c5a08d7
commit
e31fc00377
2 changed files with 36 additions and 4 deletions
|
|
@ -70,10 +70,14 @@ public class RealPathUtil {
|
|||
return uri.getLastPathSegment();
|
||||
}
|
||||
|
||||
String path = getDataColumn(context, uri, null, null);
|
||||
try {
|
||||
String path = getDataColumn(context, uri, null, null);
|
||||
|
||||
if (path != null) {
|
||||
return path;
|
||||
if (path != null) {
|
||||
return path;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// do nothing and try to get a temp file
|
||||
}
|
||||
|
||||
// Try save to tmp file, and return tmp file path
|
||||
|
|
@ -89,7 +93,12 @@ public class RealPathUtil {
|
|||
File tmpFile;
|
||||
try {
|
||||
String fileName = uri.getLastPathSegment();
|
||||
tmpFile = File.createTempFile("tmp", fileName, context.getCacheDir());
|
||||
File cacheDir = new File(context.getCacheDir(), "mmShare");
|
||||
if (!cacheDir.exists()) {
|
||||
cacheDir.mkdirs();
|
||||
}
|
||||
|
||||
tmpFile = File.createTempFile("tmp", fileName, cacheDir);
|
||||
|
||||
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
|
||||
|
||||
|
|
@ -172,4 +181,22 @@ public class RealPathUtil {
|
|||
File file = new File(filePath);
|
||||
return getMimeType(file);
|
||||
}
|
||||
|
||||
public static void deleteTempFiles(final File dir) {
|
||||
try {
|
||||
if (dir.isDirectory()) {
|
||||
deleteRecursive(dir);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteRecursive(File fileOrDirectory) {
|
||||
if (fileOrDirectory.isDirectory())
|
||||
for (File child : fileOrDirectory.listFiles())
|
||||
deleteRecursive(child);
|
||||
|
||||
fileOrDirectory.delete();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
|
|||
public ShareModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
private File tempFolder;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
|
|
@ -60,6 +61,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
|
|||
|
||||
@ReactMethod
|
||||
public void close(ReadableMap data) {
|
||||
this.clear();
|
||||
getCurrentActivity().finish();
|
||||
|
||||
if (data != null) {
|
||||
|
|
@ -78,6 +80,8 @@ public class ShareModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
RealPathUtil.deleteTempFiles(this.tempFolder);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
|
@ -96,6 +100,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
|
|||
Activity currentActivity = getCurrentActivity();
|
||||
|
||||
if (currentActivity != null) {
|
||||
this.tempFolder = new File(currentActivity.getCacheDir(), "mmShare");
|
||||
Intent intent = currentActivity.getIntent();
|
||||
action = intent.getAction();
|
||||
type = intent.getType();
|
||||
|
|
|
|||
Loading…
Reference in a new issue