mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-11-04 08:43:52 +01:00 
			
		
		
		
	Transfer createCopyPath to FileUtils as a static method
This commit is contained in:
		
							parent
							
								
									3e3956936b
								
							
						
					
					
						commit
						c97b708b0f
					
				
					 2 changed files with 25 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -26,6 +26,24 @@ import java.util.Date;
 | 
			
		|||
import timber.log.Timber;
 | 
			
		||||
 | 
			
		||||
public class FileUtils {
 | 
			
		||||
    /**
 | 
			
		||||
     * In older devices getPath() may fail depending on the source URI. Creating and using a copy of the file seems to work instead.
 | 
			
		||||
     * @return path of copy
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    static String createCopyPath(ParcelFileDescriptor descriptor) {
 | 
			
		||||
        try {
 | 
			
		||||
            String 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;
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            Timber.e(e);
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get a file path from a Uri. This will get the the path for Storage Access
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -492,31 +492,27 @@ public class ShareActivity
 | 
			
		|||
        return snackbar;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets file path from media URI.
 | 
			
		||||
     * In older devices getPath() may fail depending on the source URI, creating and using a copy of the file seems to work instead.
 | 
			
		||||
     * @return file path of media
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private String getPathOfMediaOrCopy() {
 | 
			
		||||
        String filePath = FileUtils.getPath(getApplicationContext(), mediaUri);
 | 
			
		||||
        Timber.d("Filepath: " + filePath);
 | 
			
		||||
        if (filePath == null) {
 | 
			
		||||
            // in older devices getPath() may fail depending on the source URI
 | 
			
		||||
            // creating and using a copy of the file seems to work instead.
 | 
			
		||||
            // TODO: there might be a more proper solution than this
 | 
			
		||||
            String copyPath = null;
 | 
			
		||||
            try {
 | 
			
		||||
                ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(mediaUri, "r");
 | 
			
		||||
                if (descriptor != null) {
 | 
			
		||||
                    boolean useExtStorage = prefs.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);
 | 
			
		||||
                        copyPath = FileUtils.createCopyPath(descriptor);
 | 
			
		||||
                        return copyPath;
 | 
			
		||||
                    }
 | 
			
		||||
                    copyPath = getApplicationContext().getCacheDir().getAbsolutePath() + "/" + new Date().getTime() + ".jpg";
 | 
			
		||||
                    FileUtils.copy(
 | 
			
		||||
                            descriptor.getFileDescriptor(),
 | 
			
		||||
                            copyPath);
 | 
			
		||||
                    FileUtils.copy(descriptor.getFileDescriptor(), copyPath);
 | 
			
		||||
                    Timber.d("Filepath (copied): %s", copyPath);
 | 
			
		||||
                    return copyPath;
 | 
			
		||||
                }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue