Skip to content

Commit

Permalink
Moved getFileExtensionFromUri() method to ContentResolverHelper (#2567)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobhitagarwal1612 authored and lognaturel committed Oct 1, 2018
1 parent ad63947 commit e0a156f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
Expand Down Expand Up @@ -879,10 +878,9 @@ public void run() {
* This may take a long time.
*
* @param selectedFile uri of the selected audio
* @see #getFileExtensionFromUri(Uri)
*/
private void saveChosenFile(Uri selectedFile) {
String extension = getFileExtensionFromUri(selectedFile);
String extension = ContentResolverHelper.getFileExtensionFromUri(selectedFile);

String instanceFolder = Collect.getInstance().getFormController()
.getInstanceFile().getParent();
Expand Down Expand Up @@ -925,40 +923,6 @@ public void run() {
}
}

/**
* Using contentResolver to get a file's extension by the uri returned from OnActivityResult.
*
* @param fileUri Whose name we want to get
* @return The file's extension
* @see #onActivityResult(int, int, Intent)
* @see #saveChosenFile(Uri)
* @see android.content.ContentResolver
*/
private String getFileExtensionFromUri(Uri fileUri) {
Cursor returnCursor =
getContentResolver().query(fileUri, null, null, null, null);
try {
if (returnCursor == null) {
return "";
}
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String filename = returnCursor.getString(nameIndex);
// If the file's name contains extension , we cut it down for latter use (copy a new file).
if (filename.lastIndexOf('.') != -1) {
return filename.substring(filename.lastIndexOf('.'));
} else {
// I found some mp3 files' name don't contain extension, but can be played as Audio
// So I write so, but I still there are some way to get its extension
return "";
}
} finally {
if (returnCursor != null) {
returnCursor.close();
}
}
}

public QuestionWidget getWidgetWaitingForBinaryData() {
QuestionWidget questionWidget = null;
ODKView odkView = (ODKView) currentView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;

import org.odk.collect.android.application.Collect;
import org.odk.collect.android.logic.FormInfo;
Expand All @@ -29,12 +30,14 @@ private ContentResolverHelper() {

}

private static ContentResolver getContentResolver() {
return Collect.getInstance().getContentResolver();
}

public static FormInfo getFormDetails(Uri uri) {
FormInfo formInfo = null;

ContentResolver contentResolver = Collect.getInstance().getContentResolver();

try (Cursor instanceCursor = contentResolver.query(uri, null, null, null, null)) {
try (Cursor instanceCursor = getContentResolver().query(uri, null, null, null, null)) {
if (instanceCursor != null && instanceCursor.getCount() > 0) {
instanceCursor.moveToFirst();
String instancePath = instanceCursor
Expand All @@ -59,13 +62,34 @@ public static FormInfo getFormDetails(Uri uri) {

public static String getFormPath(Uri uri) {
String formPath = null;
ContentResolver contentResolver = Collect.getInstance().getContentResolver();
try (Cursor c = contentResolver.query(uri, null, null, null, null)) {
try (Cursor c = getContentResolver().query(uri, null, null, null, null)) {
if (c != null && c.getCount() == 1) {
c.moveToFirst();
formPath = c.getString(c.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_FILE_PATH));
}
}
return formPath;
}


/**
* Using contentResolver to get a file's extension by the uri
*
* @param fileUri Whose name we want to get
* @return The file's extension
*/
public static String getFileExtensionFromUri(Uri fileUri) {
try (Cursor returnCursor = getContentResolver().query(fileUri, null, null, null, null)) {
if (returnCursor != null) {
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String filename = returnCursor.getString(nameIndex);
// If the file's name contains extension , we cut it down for latter use (copy a new file).
if (filename.lastIndexOf('.') != -1) {
return filename.substring(filename.lastIndexOf('.'));
}
}
}
return "";
}
}

0 comments on commit e0a156f

Please sign in to comment.