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,6 +58,7 @@ 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) {
if (editView != null)
editView.setMarkdown(getMarkdown()); editView.setMarkdown(getMarkdown());
onMarkdownEdited(); onMarkdownEdited();
} else { } else {
@ -160,6 +159,7 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
try { try {
InputStream in = InputStream in =
context.getContentResolver().openInputStream(fileUri); context.getContentResolver().openInputStream(fileUri);
if (fileUri.getScheme().equals("content")) {
Cursor retCur = context.getContentResolver() Cursor retCur = context.getContentResolver()
.query(fileUri, null, null, null, null); .query(fileUri, null, null, null, null);
if (retCur != null) { if (retCur != null) {
@ -168,10 +168,12 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
retCur.moveToFirst(); retCur.moveToFirst();
setFileName(retCur.getString(nameIndex)); 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();
} }
} }
} }