mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-30 22:34:02 +01:00 
			
		
		
		
	Made changes to ensure that when attempting to upload multiple images, the GPS data is extracted and attached correctly.
This commit is contained in:
		
							parent
							
								
									88f0e65bcc
								
							
						
					
					
						commit
						f284a00de2
					
				
					 3 changed files with 118 additions and 8 deletions
				
			
		|  | @ -197,6 +197,10 @@ public class Contribution extends Media { | |||
|         this.localUri = localUri; | ||||
|     } | ||||
| 
 | ||||
|     public void setDecimalCoords(String decimalCoords) { | ||||
|         this.decimalCoords = decimalCoords; | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     private String licenseTemplateFor(String license) { | ||||
|         switch (license) { | ||||
|  | @ -215,6 +219,7 @@ public class Contribution extends Media { | |||
|             case Prefs.Licenses.CC_BY_SA: | ||||
|                 return "{{self|cc-by-sa-3.0}}"; | ||||
|         } | ||||
| 
 | ||||
|         throw new RuntimeException("Unrecognized license value: " + license); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -3,21 +3,27 @@ package fr.free.nrw.commons.upload; | |||
| import android.annotation.SuppressLint; | ||||
| import android.content.ContentUris; | ||||
| import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.database.Cursor; | ||||
| import android.net.Uri; | ||||
| import android.os.Build; | ||||
| import android.os.Environment; | ||||
| import android.os.ParcelFileDescriptor; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.provider.DocumentsContract; | ||||
| import android.provider.MediaStore; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.FileDescriptor; | ||||
| import java.io.FileInputStream; | ||||
| import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.nio.channels.FileChannel; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import fr.free.nrw.commons.CommonsApplication; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| public class FileUtils { | ||||
|  | @ -85,8 +91,41 @@ public class FileUtils { | |||
|         // File | ||||
|         else if ("file".equalsIgnoreCase(uri.getScheme())) { | ||||
|             return uri.getPath(); | ||||
|         } else { | ||||
|             //fetching path may fail depending on the source URI and all hope is lost | ||||
|             //so we will create and use a copy of the file, which seems to work | ||||
|             String copyPath = null; | ||||
|             try { | ||||
|                 ParcelFileDescriptor descriptor | ||||
|                         = context.getContentResolver().openFileDescriptor(uri, "r"); | ||||
|                 if (descriptor != null) { | ||||
|                     SharedPreferences sharedPref = PreferenceManager | ||||
|                             .getDefaultSharedPreferences(CommonsApplication.getInstance()); | ||||
|                     boolean useExtStorage = sharedPref.getBoolean("useExternalStorage", true); | ||||
|                     if (useExtStorage) { | ||||
|                         copyPath = Environment.getExternalStorageDirectory().toString() | ||||
|                                 + "/CommonsApp/" + new Date().getTime() + ".jpg"; | ||||
|                         File newFile = new File(Environment.getExternalStorageDirectory().toString() + "/CommonsApp"); | ||||
|                         newFile.mkdir(); | ||||
|                         FileUtils.copy( | ||||
|                                 descriptor.getFileDescriptor(), | ||||
|                                 copyPath); | ||||
|                         Timber.d("Filepath (copied): %s", copyPath); | ||||
|                         return copyPath; | ||||
|                     } | ||||
|                     copyPath = context.getCacheDir().getAbsolutePath() | ||||
|                             + "/" + new Date().getTime() + ".jpg"; | ||||
|                     FileUtils.copy( | ||||
|                             descriptor.getFileDescriptor(), | ||||
|                             copyPath); | ||||
|                     Timber.d("Filepath (copied): %s", copyPath); | ||||
|                     return copyPath; | ||||
|                 } | ||||
|             } catch (IOException e) { | ||||
|                 Timber.w(e, "Error in file " + copyPath); | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -11,7 +11,9 @@ import android.database.DataSetObserver; | |||
| import android.net.Uri; | ||||
| import android.os.Build; | ||||
| import android.os.Bundle; | ||||
| import android.os.ParcelFileDescriptor; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.support.v4.app.ActivityCompat; | ||||
| import android.support.v4.app.FragmentManager; | ||||
| import android.support.v4.content.ContextCompat; | ||||
|  | @ -21,6 +23,7 @@ import android.view.inputmethod.InputMethodManager; | |||
| import android.widget.AdapterView; | ||||
| import android.widget.Toast; | ||||
| 
 | ||||
| import java.io.FileNotFoundException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
|  | @ -51,11 +54,17 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
|         MultipleUploadListFragment.OnMultipleUploadInitiatedHandler, | ||||
|         OnCategoriesSaveHandler { | ||||
| 
 | ||||
|     @Inject MediaWikiApi mwApi; | ||||
|     @Inject SessionManager sessionManager; | ||||
|     @Inject UploadController uploadController; | ||||
|     @Inject ModifierSequenceDao modifierSequenceDao; | ||||
|     @Inject @Named("default_preferences") SharedPreferences prefs; | ||||
|     @Inject | ||||
|     MediaWikiApi mwApi; | ||||
|     @Inject | ||||
|     SessionManager sessionManager; | ||||
|     @Inject | ||||
|     UploadController uploadController; | ||||
|     @Inject | ||||
|     ModifierSequenceDao modifierSequenceDao; | ||||
|     @Inject | ||||
|     @Named("default_preferences") | ||||
|     SharedPreferences prefs; | ||||
| 
 | ||||
|     private ArrayList<Contribution> photosList = null; | ||||
| 
 | ||||
|  | @ -63,6 +72,8 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
|     private MediaDetailPagerFragment mediaDetails; | ||||
|     private CategorizationFragment categorizationFragment; | ||||
| 
 | ||||
|     private boolean locationPermitted = false; | ||||
| 
 | ||||
|     @Override | ||||
|     public Media getMediaAtPosition(int i) { | ||||
|         return photosList.get(i); | ||||
|  | @ -207,6 +218,14 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
| 
 | ||||
|         getSupportFragmentManager().addOnBackStackChangedListener(this); | ||||
|         requestAuthToken(); | ||||
| 
 | ||||
|         //TODO: 15/10/17 should location permission be explicitly requested if not provided? | ||||
|         //check if location permission is enabled | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||||
|             if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | ||||
|                 locationPermitted = true; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | @ -240,7 +259,7 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
|         mwApi.setAuthCookie(authCookie); | ||||
|         Intent intent = getIntent(); | ||||
| 
 | ||||
|         if (intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) { | ||||
|         if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) { | ||||
|             if (photosList == null) { | ||||
|                 photosList = new ArrayList<>(); | ||||
|                 ArrayList<Uri> urisList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); | ||||
|  | @ -252,6 +271,11 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
|                     up.setTag("sequence", i); | ||||
|                     up.setSource(Contribution.SOURCE_EXTERNAL); | ||||
|                     up.setMultiple(true); | ||||
|                     String imageGpsCoordinates = extractImageGpsData(uri); | ||||
|                     if (imageGpsCoordinates != null) { | ||||
|                         Timber.d("GPS data for image found!"); | ||||
|                         up.setDecimalCoords(imageGpsCoordinates); | ||||
|                     } | ||||
|                     photosList.add(up); | ||||
|                 } | ||||
|             } | ||||
|  | @ -278,7 +302,49 @@ public class MultipleShareActivity extends AuthenticatedActivity | |||
| 
 | ||||
|     @Override | ||||
|     public void onBackStackChanged() { | ||||
|         getSupportActionBar().setDisplayHomeAsUpEnabled(mediaDetails != null && mediaDetails.isVisible()) ; | ||||
|         getSupportActionBar().setDisplayHomeAsUpEnabled(mediaDetails != null && mediaDetails.isVisible()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Will attempt to extract the gps coordinates using exif data or by using the current | ||||
|      * location if available for the image who's imageUri has been provided. | ||||
|      * @param imageUri The uri of the image who's GPS coordinates data we wish to extract | ||||
|      * @return GPS coordinates as a String as is returned by {@link GPSExtractor} | ||||
|      */ | ||||
|     @Nullable | ||||
|     private String extractImageGpsData(Uri imageUri) { | ||||
|         Timber.d("Entering extractImagesGpsData"); | ||||
| 
 | ||||
|         if (imageUri == null) { | ||||
|             //now why would you do that??? | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         GPSExtractor gpsExtractor = null; | ||||
| 
 | ||||
|         try { | ||||
|             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||||
|                 ParcelFileDescriptor fd = getContentResolver().openFileDescriptor(imageUri,"r"); | ||||
|                 if (fd != null) { | ||||
|                     gpsExtractor = new GPSExtractor(fd.getFileDescriptor(),this,prefs); | ||||
|                 } | ||||
|             } else { | ||||
|                 String filePath = FileUtils.getPath(this,imageUri); | ||||
|                 if (filePath != null) { | ||||
|                     gpsExtractor = new GPSExtractor(filePath,this,prefs); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             if (gpsExtractor != null) { | ||||
|                 //get image coordinates from exif data or user location | ||||
|                 return gpsExtractor.getCoords(locationPermitted); | ||||
|             } | ||||
| 
 | ||||
|         } catch (FileNotFoundException fnfe) { | ||||
|             Timber.w(fnfe); | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vishan Seru
						Vishan Seru