Fixed NullPointerException on setMarkdown call and fixed file name not loading correctly for file:: URIs

This commit is contained in:
William Brawner 2017-08-29 20:33:13 -06:00 committed by William Brawner
parent f3a6359a4f
commit b0490105b3

View file

@ -4,10 +4,8 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.widget.Toast;
import com.commonsware.cwac.anddown.AndDown; import com.commonsware.cwac.anddown.AndDown;
import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.model.MarkdownFile; import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.view.MarkdownEditView; import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView; import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
@ -60,7 +58,8 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
Runnable fileLoader = () -> { Runnable fileLoader = () -> {
int result = file.load(in); int result = file.load(in);
if (result == MarkdownFile.SUCCESS) { if (result == MarkdownFile.SUCCESS) {
editView.setMarkdown(getMarkdown()); if (editView != null)
editView.setMarkdown(getMarkdown());
onMarkdownEdited(); onMarkdownEdited();
} else { } else {
editView.showFileLoadeddError(result); editView.showFileLoadeddError(result);
@ -160,18 +159,21 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
try { try {
InputStream in = InputStream in =
context.getContentResolver().openInputStream(fileUri); context.getContentResolver().openInputStream(fileUri);
Cursor retCur = context.getContentResolver() if (fileUri.getScheme().equals("content")) {
.query(fileUri, null, null, null, null); Cursor retCur = context.getContentResolver()
if (retCur != null) { .query(fileUri, null, null, null, null);
int nameIndex = retCur if (retCur != null) {
.getColumnIndex(OpenableColumns.DISPLAY_NAME); int nameIndex = retCur
retCur.moveToFirst(); .getColumnIndex(OpenableColumns.DISPLAY_NAME);
setFileName(retCur.getString(nameIndex)); retCur.moveToFirst();
setFileName(retCur.getString(nameIndex));
}
} else if (fileUri.getScheme().equals("file")) {
setFileName(fileUri.getLastPathSegment());
} }
loadMarkdown(in); loadMarkdown(in);
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(context, R.string.file_load_error, Toast.LENGTH_SHORT) editView.showFileLoadeddError(MarkdownFile.READ_ERROR);
.show();
} }
} }
} }