Skip to content

Commit

Permalink
added: local files shared in android
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanFuentealba committed Jul 22, 2016
1 parent ebb1364 commit 797dc89
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
11 changes: 1 addition & 10 deletions android/src/main/java/cl/json/RNShareModule.java
Expand Up @@ -5,19 +5,13 @@
import android.content.ActivityNotFoundException;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

public class RNShareModule extends ReactContextBaseJavaModule {
Expand Down Expand Up @@ -69,16 +63,13 @@ private Intent createShareIntent(ReadableMap options) {
}
//isPackageInstalled("com.whatsapp", this.reactContext);
if (hasValidKey("message", options) && hasValidKey("url", options)) {
FileBase64 fileShare = new FileBase64(options.getString("url"));
ShareFile fileShare = new ShareFile(options.getString("url"), this.reactContext);
if(fileShare.isFile()) {
Uri uriFile = fileShare.getURI();
intent.setType(fileShare.getType());
System.out.println("es base 64 file");
System.out.printf(options.getString("url"));
intent.putExtra(Intent.EXTRA_STREAM, uriFile);
intent.putExtra(Intent.EXTRA_TEXT, options.getString("message"));
} else {
System.out.println("no es base 64 file");
intent.putExtra(Intent.EXTRA_TEXT, options.getString("message") + " " + options.getString("url"));
}
} else if (hasValidKey("url", options)) {
Expand Down
@@ -1,11 +1,17 @@
package cl.json;

import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.webkit.MimeTypeMap;

import com.facebook.react.bridge.ReactApplicationContext;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -15,17 +21,20 @@
/**
* Created by disenodosbbcl on 22-07-16.
*/
public class FileBase64 {
public class ShareFile {

private final ReactApplicationContext reactContext;
private String url;
private URI uri;
private Uri uri;
private String type = "*/*";
private String extension = "";

public FileBase64(String url){
public ShareFile(String url, ReactApplicationContext reactContext){
this.url = url;
this.uri = URI.create(this.url);
this.uri = Uri.parse(this.url);
this.reactContext = reactContext;
}
private static String getMimeType(String url) {
private String getMimeType(String url) {
String type = "*/*";
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
Expand All @@ -34,27 +43,46 @@ private static String getMimeType(String url) {
return type;
}
public boolean isFile() {
return this.isBase64File();
return this.isBase64File() || this.isLocalFile();
}
public boolean isBase64File() {
if(uri.getScheme().equals("data")) {
this.type = this.uri.getSchemeSpecificPart().substring(0, this.uri.getSchemeSpecificPart().indexOf(";"));
final MimeTypeMap mime = MimeTypeMap.getSingleton();
this.extension = mime.getExtensionFromMimeType(this.type);
return true;
}
return false;
}
public boolean isLocalFile() {
if(uri.getScheme().equals("content") || uri.getScheme().equals("file")) {
String realPath = this.getRealPathFromURI(uri);
this.type = this.getMimeType(realPath);

return true;
}
return false;
}
public String getType() {
return this.type;
}
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(this.reactContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
}
public Uri getURI() {

final MimeTypeMap mime = MimeTypeMap.getSingleton();
this.extension = mime.getExtensionFromMimeType(this.type);
if(this.isBase64File()) {
String encodedImg = this.uri.getSchemeSpecificPart().substring(this.uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists())
{
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, System.currentTimeMillis() + "." + this.extension);
Expand All @@ -67,6 +95,10 @@ public Uri getURI() {
} catch (IOException e) {
e.printStackTrace();
}
} else if(this.isLocalFile()) {
Uri uri = Uri.parse(this.url);

return uri;
}

return null;
Expand Down

0 comments on commit 797dc89

Please sign in to comment.