mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
Fix date time issue for google photos (#2438)
This commit is contained in:
parent
d86a3aad79
commit
2bc0d41748
9 changed files with 62 additions and 59 deletions
|
|
@ -289,20 +289,20 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
private static File takenCameraPicture(Context context) throws IOException, URISyntaxException {
|
||||
private static UploadableFile takenCameraPicture(Context context) throws IOException, URISyntaxException {
|
||||
String lastCameraPhoto = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_PHOTO, null);
|
||||
if (lastCameraPhoto != null) {
|
||||
return new File(lastCameraPhoto);
|
||||
return new UploadableFile(new File(lastCameraPhoto));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static File takenCameraVideo(Context context) throws IOException, URISyntaxException {
|
||||
private static UploadableFile takenCameraVideo(Context context) throws IOException, URISyntaxException {
|
||||
String lastCameraPhoto = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_VIDEO, null);
|
||||
if (lastCameraPhoto != null) {
|
||||
return new File(lastCameraPhoto);
|
||||
return new UploadableFile(new File(lastCameraPhoto));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -343,7 +343,7 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<File> handleExternalImagesPicked(Intent data, Activity activity) {
|
||||
public static List<UploadableFile> handleExternalImagesPicked(Intent data, Activity activity) {
|
||||
try {
|
||||
return getFilesFromGalleryPictures(data, activity);
|
||||
} catch (IOException e) {
|
||||
|
|
@ -406,7 +406,7 @@ public class FilePicker implements Constants {
|
|||
private static void onPictureReturnedFromDocuments(Intent data, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
|
||||
try {
|
||||
Uri photoPath = data.getData();
|
||||
File photoFile = PickedFiles.pickedExistingPicture(activity, photoPath);
|
||||
UploadableFile photoFile = PickedFiles.pickedExistingPicture(activity, photoPath);
|
||||
callbacks.onImagesPicked(singleFileList(photoFile), FilePicker.ImageSource.DOCUMENTS, restoreType(activity));
|
||||
|
||||
if (configuration(activity).shouldCopyPickedImagesToPublicGalleryAppFolder()) {
|
||||
|
|
@ -420,7 +420,7 @@ public class FilePicker implements Constants {
|
|||
|
||||
private static void onPictureReturnedFromGallery(Intent data, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
|
||||
try {
|
||||
List<File> files = getFilesFromGalleryPictures(data, activity);
|
||||
List<UploadableFile> files = getFilesFromGalleryPictures(data, activity);
|
||||
callbacks.onImagesPicked(files, FilePicker.ImageSource.GALLERY, restoreType(activity));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -428,20 +428,20 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
}
|
||||
|
||||
private static List<File> getFilesFromGalleryPictures(Intent data, Activity activity) throws IOException {
|
||||
List<File> files = new ArrayList<>();
|
||||
private static List<UploadableFile> getFilesFromGalleryPictures(Intent data, Activity activity) throws IOException {
|
||||
List<UploadableFile> files = new ArrayList<>();
|
||||
ClipData clipData = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||
clipData = data.getClipData();
|
||||
}
|
||||
if (clipData == null) {
|
||||
Uri uri = data.getData();
|
||||
File file = PickedFiles.pickedExistingPicture(activity, uri);
|
||||
UploadableFile file = PickedFiles.pickedExistingPicture(activity, uri);
|
||||
files.add(file);
|
||||
} else {
|
||||
for (int i = 0; i < clipData.getItemCount(); i++) {
|
||||
Uri uri = clipData.getItemAt(i).getUri();
|
||||
File file = PickedFiles.pickedExistingPicture(activity, uri);
|
||||
UploadableFile file = PickedFiles.pickedExistingPicture(activity, uri);
|
||||
files.add(file);
|
||||
}
|
||||
}
|
||||
|
|
@ -460,8 +460,8 @@ public class FilePicker implements Constants {
|
|||
revokeWritePermission(activity, Uri.parse(lastImageUri));
|
||||
}
|
||||
|
||||
File photoFile = FilePicker.takenCameraPicture(activity);
|
||||
List<File> files = new ArrayList<>();
|
||||
UploadableFile photoFile = FilePicker.takenCameraPicture(activity);
|
||||
List<UploadableFile> files = new ArrayList<>();
|
||||
files.add(photoFile);
|
||||
|
||||
if (photoFile == null) {
|
||||
|
|
@ -493,8 +493,8 @@ public class FilePicker implements Constants {
|
|||
revokeWritePermission(activity, Uri.parse(lastVideoUri));
|
||||
}
|
||||
|
||||
File photoFile = FilePicker.takenCameraVideo(activity);
|
||||
List<File> files = new ArrayList<>();
|
||||
UploadableFile photoFile = FilePicker.takenCameraVideo(activity);
|
||||
List<UploadableFile> files = new ArrayList<>();
|
||||
files.add(photoFile);
|
||||
|
||||
if (photoFile == null) {
|
||||
|
|
@ -545,7 +545,7 @@ public class FilePicker implements Constants {
|
|||
public interface Callbacks {
|
||||
void onImagePickerError(Exception e, FilePicker.ImageSource source, int type);
|
||||
|
||||
void onImagesPicked(@NonNull List<File> imageFiles, FilePicker.ImageSource source, int type);
|
||||
void onImagesPicked(@NonNull List<UploadableFile> imageFiles, FilePicker.ImageSource source, int type);
|
||||
|
||||
void onCanceled(FilePicker.ImageSource source, int type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,13 +55,14 @@ class PickedFiles implements Constants {
|
|||
writeToFile(in, dst);
|
||||
}
|
||||
|
||||
static void copyFilesInSeparateThread(final Context context, final List<File> filesToCopy) {
|
||||
static void copyFilesInSeparateThread(final Context context, final List<UploadableFile> filesToCopy) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<File> copiedFiles = new ArrayList<>();
|
||||
int i = 1;
|
||||
for (File fileToCopy : filesToCopy) {
|
||||
for (UploadableFile uploadableFile : filesToCopy) {
|
||||
File fileToCopy = uploadableFile.getFile();
|
||||
File dstDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getFolderName(context));
|
||||
if (!dstDir.exists()) dstDir.mkdirs();
|
||||
|
||||
|
|
@ -84,8 +85,8 @@ class PickedFiles implements Constants {
|
|||
}).run();
|
||||
}
|
||||
|
||||
static List<File> singleFileList(File file) {
|
||||
List<File> list = new ArrayList<>();
|
||||
static List<UploadableFile> singleFileList(UploadableFile file) {
|
||||
List<UploadableFile> list = new ArrayList<>();
|
||||
list.add(file);
|
||||
return list;
|
||||
}
|
||||
|
|
@ -106,13 +107,13 @@ class PickedFiles implements Constants {
|
|||
});
|
||||
}
|
||||
|
||||
static File pickedExistingPicture(@NonNull Context context, Uri photoUri) throws IOException {
|
||||
static UploadableFile pickedExistingPicture(@NonNull Context context, Uri photoUri) throws IOException {
|
||||
InputStream pictureInputStream = context.getContentResolver().openInputStream(photoUri);
|
||||
File directory = tempImageDirectory(context);
|
||||
File photoFile = new File(directory, UUID.randomUUID().toString() + "." + getMimeType(context, photoUri));
|
||||
photoFile.createNewFile();
|
||||
writeToFile(pictureInputStream, photoFile);
|
||||
return photoFile;
|
||||
return new UploadableFile(photoUri, photoFile);
|
||||
}
|
||||
|
||||
static File getCameraPicturesLocation(@NonNull Context context) throws IOException {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
package fr.free.nrw.commons.filepicker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import fr.free.nrw.commons.upload.FileUtils;
|
||||
|
||||
public class UploadableFile implements Parcelable {
|
||||
public static final Creator<UploadableFile> CREATOR = new Creator<UploadableFile>() {
|
||||
@Override
|
||||
public UploadableFile createFromParcel(Parcel in) {
|
||||
return new UploadableFile(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadableFile[] newArray(int size) {
|
||||
return new UploadableFile[size];
|
||||
}
|
||||
};
|
||||
|
||||
private final Uri contentUri;
|
||||
private final File file;
|
||||
|
||||
public UploadableFile(Uri contentUri, File file) {
|
||||
this.contentUri = contentUri;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public UploadableFile(File file) {
|
||||
this.file = file;
|
||||
this.contentUri = Uri.parse(file.getAbsolutePath());
|
||||
}
|
||||
|
||||
public UploadableFile(Parcel in) {
|
||||
this.contentUri = in.readParcelable(Uri.class.getClassLoader());
|
||||
file = (File) in.readSerializable();
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return file.getPath();
|
||||
}
|
||||
|
||||
public Uri getMediaUri() {
|
||||
return Uri.parse(getFilePath());
|
||||
}
|
||||
|
||||
public String getMimeType(Context context) {
|
||||
return FileUtils.getMimeType(context, getMediaUri());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filePath creation date from uri from all possible content providers
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public long getFileCreatedDate(Context context) {
|
||||
try {
|
||||
Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
|
||||
if (cursor == null) {
|
||||
return -1;//Could not fetch last_modified
|
||||
}
|
||||
//Content provider contracts for opening gallery from the app and that by sharing from gallery from outside are different and we need to handle both the cases
|
||||
int lastModifiedColumnIndex = cursor.getColumnIndex("last_modified");//If gallery is opened from in app
|
||||
if (lastModifiedColumnIndex == -1) {
|
||||
lastModifiedColumnIndex = cursor.getColumnIndex("datetaken");
|
||||
}
|
||||
//If both the content providers do not give the data, lets leave it to Jesus
|
||||
if (lastModifiedColumnIndex == -1) {
|
||||
return -1l;
|
||||
}
|
||||
cursor.moveToFirst();
|
||||
return cursor.getLong(lastModifiedColumnIndex);
|
||||
} catch (Exception e) {
|
||||
return -1;////Could not fetch last_modified
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
parcel.writeParcelable(contentUri, 0);
|
||||
parcel.writeSerializable(file);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue