[File sharing] fix file picking in chat

This commit is contained in:
Brieuc Viel 2017-08-31 15:10:07 +02:00
parent ee32ce38e7
commit 678655ecd8
2 changed files with 36 additions and 17 deletions

View file

@ -756,7 +756,9 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
if(path.contains("file://")) { if(path.contains("file://")) {
path = path.substring(7); path = path.substring(7);
} }
if(path.contains("%20")) {
path = path.replace("%20", "-");
}
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable();
if(newChatConversation && chatRoom == null) { if(newChatConversation && chatRoom == null) {
@ -765,7 +767,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
initChatRoom(address); initChatRoom(address);
} }
} }
if (chatRoom != null && path != null && path.length() > 0 && isNetworkReachable) { if (chatRoom != null && path != null && path.length() > 0 && isNetworkReachable) {
try { try {
Bitmap bm = BitmapFactory.decodeFile(path); Bitmap bm = BitmapFactory.decodeFile(path);
@ -785,12 +786,17 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
private void sendFileSharingMessage(String path, int size ) { private void sendFileSharingMessage(String path, int size ) {
if(path.contains("file://")) { if (path.contains("file://")) {
path = path.substring(7); path = path.substring(7);
}else if(path.contains("com.android.contacts/contacts/")){ } else if (path.contains("com.android.contacts/contacts/")) {
path = getCVSPathFromLookupUri(path).toString(); path = getCVSPathFromLookupUri(path).toString();
} else if(path.contains("vcard") || path.contains("vcf")) { } else if (path.contains("vcard") || path.contains("vcf")) {
path = (LinphoneUtils.createCvsFromString(LinphoneActivity.instance().getCVSPathFromOtherUri(path).toString())).toString(); path = (LinphoneUtils.createCvsFromString(LinphoneActivity.instance().getCVSPathFromOtherUri(path).toString())).toString();
} else if (path.contains("content://")){
path = LinphoneUtils.getFilePath(this.getActivity().getApplicationContext(), Uri.parse(path));
}
if(path.contains("%20")) {
path = path.replace("%20", "-");
} }
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable();
@ -913,15 +919,20 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private void pickImage() { private void pickImage() {
List<Intent> cameraIntents = new ArrayList<Intent>(); List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis()))); File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.temp_photo_name_with_date).replace("%s", String.valueOf(System.currentTimeMillis())+".jpeg"));
imageToUploadUri = Uri.fromFile(file); imageToUploadUri = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri);
cameraIntents.add(captureIntent); cameraIntents.add(captureIntent);
Intent galleryIntent = new Intent(); Intent galleryIntent = new Intent();
galleryIntent.setType("*/*"); galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK); galleryIntent.setAction(Intent.ACTION_PICK);
Intent fileIntent = new Intent();
fileIntent.setType("*/*");
fileIntent.setAction(Intent.ACTION_GET_CONTENT);
cameraIntents.add(fileIntent);
Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.image_picker_title)); Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.image_picker_title));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
@ -1124,7 +1135,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data != null) { if(data != null) {
if (requestCode == ADD_PHOTO && resultCode == Activity.RESULT_OK) { if (requestCode == ADD_PHOTO && resultCode == Activity.RESULT_OK) {
String fileToUploadPath = null; String fileToUploadPath = null;
if (data != null && data.getData() != null) { if (data != null && data.getData() != null) {
@ -1138,8 +1149,9 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} else { } else {
fileToUploadPath = getRealPathFromURI(data.getData()); fileToUploadPath = getRealPathFromURI(data.getData());
} }
if (fileToUploadPath == null) if (fileToUploadPath == null) {
fileToUploadPath = data.getData().toString(); fileToUploadPath = data.getData().toString();
}
} else if (imageToUploadUri != null) { } else if (imageToUploadUri != null) {
fileToUploadPath = imageToUploadUri.getPath(); fileToUploadPath = imageToUploadUri.getPath();
} }
@ -1518,7 +1530,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
loadBitmap(appData, holder.messageImage); loadBitmap(appData, holder.messageImage);
holder.messageImage.setTag(message.getAppData()); holder.messageImage.setTag(message.getAppData());
} }
} }
} }
} }
@ -1628,6 +1639,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
file = new File(imageUri); file = new File(imageUri);
contentUri = FileProvider.getUriForFile(getActivity(), "org.linphone.provider", file); contentUri = FileProvider.getUriForFile(getActivity(), "org.linphone.provider", file);
} else if (imageUri.startsWith("content://")) { } else if (imageUri.startsWith("content://")) {
Log.e("===>>> ChatFragment - getView() - imageUri = "+imageUri);
contentUri = Uri.parse(imageUri); contentUri = Uri.parse(imageUri);
} else { } else {
file = new File(imageUri); file = new File(imageUri);

View file

@ -43,6 +43,7 @@ import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
@ -738,16 +739,15 @@ public final class LinphoneUtils {
************************************************************************************************/ ************************************************************************************************/
public static String getFilePath(final Context context, final Uri uri) { public static String getFilePath(final Context context, final Uri uri) {
// Google photo uri example // Google photo uri example
// content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2FAF1QipMObgoK_wDY66gu0QkMAi/ORIGINAL/NONE/114919 // content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2FAF1QipMObgoK_wDY66gu0QkMAi/ORIGINAL/NONE/114919
if ("content".equalsIgnoreCase(uri.getScheme())) { if ("content".equalsIgnoreCase(uri.getScheme())) {
String type = getTypeFromUri(uri, context);
String result = getDataColumn(context, uri, null, null); // String result = getDataColumn(context, uri, null, null); //
if (TextUtils.isEmpty(result)) if (TextUtils.isEmpty(result))
if (uri.getAuthority().contains("com.google.android")) { if (uri.getAuthority().contains("com.google.android") || uri.getAuthority().contains("com.android")) {
try { try {
File localFile = createImageFile(context, null); File localFile = createFile(context, null, type);
FileInputStream remoteFile = getSourceStream(context, uri); FileInputStream remoteFile = getSourceStream(context, uri);
if(copyToFile(remoteFile, localFile)) if(copyToFile(remoteFile, localFile))
result = localFile.getAbsolutePath(); result = localFile.getAbsolutePath();
@ -762,10 +762,17 @@ public final class LinphoneUtils {
else if ("file".equalsIgnoreCase(uri.getScheme())) { else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath(); return uri.getPath();
} }
return null; return null;
} }
private static String getTypeFromUri(Uri uri, Context context){
ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getExtensionFromMimeType(cR.getType(uri));
return type;
}
/** /**
* Copy data from a source stream to destFile. * Copy data from a source stream to destFile.
* Return true if succeed, return false if failed. * Return true if succeed, return false if failed.
@ -797,9 +804,9 @@ public final class LinphoneUtils {
} }
} }
public static File createImageFile(Context context, String imageFileName) throws IOException { public static File createFile(Context context, String imageFileName, String type) throws IOException {
if (TextUtils.isEmpty(imageFileName)) if (TextUtils.isEmpty(imageFileName))
imageFileName = getTimestamp()+".JPEG"; // make random filename if you want. imageFileName = getTimestamp()+"."+type; // make random filename if you want.
final File root; final File root;
imageFileName = imageFileName; imageFileName = imageFileName;