Skip to content

Commit

Permalink
TXT: Differentiate File and ContentProvider Uri by parsing it
Browse files Browse the repository at this point in the history
  • Loading branch information
VishalNehra committed Jan 30, 2017
1 parent c0980c0 commit 4f6885b
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions src/main/java/com/amaze/filemanager/activities/TextReader.java
Expand Up @@ -383,29 +383,31 @@ private void writeTextFile(final Uri uri, final File file, String inputText)

OutputStream outputStream = null;

if(file.canWrite()) {
try {
if (uri.toString().contains("file://")) {

outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
outputStream = null;
// dealing with files
if(file.canWrite()) {
try {

outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
outputStream = null;
}
}
}

if (BaseActivity.rootMode && outputStream == null) {
// try loading stream associated using root
try {
if (BaseActivity.rootMode && outputStream == null) {
// try loading stream associated using root
try {

if (cacheFile != null && cacheFile.exists())
outputStream = new FileOutputStream(cacheFile);
if (cacheFile != null && cacheFile.exists())
outputStream = new FileOutputStream(cacheFile);

} catch (FileNotFoundException e) {
e.printStackTrace();
outputStream = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
outputStream = null;
}
}
}

if (outputStream == null) {
} else if (uri.toString().contains("content://")) {

if (parcelFileDescriptor != null) {
File descriptorFile = new File(GenericCopyUtil.PATH_FILE_DESCRIPTOR + parcelFileDescriptor.getFd());
Expand Down Expand Up @@ -645,38 +647,42 @@ private InputStream getInputStream(Uri uri, File file)
throws StreamNotFoundException {
InputStream stream = null;

if (!file.canWrite() && BaseActivity.rootMode) {

// try loading stream associated using root
if (uri.toString().contains("file://")) {

try {
// dealing with files
if (!file.canWrite() && BaseActivity.rootMode) {

File cacheDir = getExternalCacheDir();
// try loading stream associated using root

cacheFile = new File(cacheDir, mFile.getName());
// creating a cache file
RootUtils.copy(mFile.getPath(), cacheFile.getPath());
try {
stream = new FileInputStream(cacheFile);
} catch (FileNotFoundException e) {

File cacheDir = getExternalCacheDir();

cacheFile = new File(cacheDir, mFile.getName());
// creating a cache file
RootUtils.copy(mFile.getPath(), cacheFile.getPath());
try {
stream = new FileInputStream(cacheFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
stream = null;
}
} catch (RootNotPermittedException e) {
e.printStackTrace();
stream = null;
}
} catch (RootNotPermittedException e) {
e.printStackTrace();
stream = null;
}
} else if (file.canRead()) {
} else if (file.canRead()) {

// readable file in filesystem
try {
stream=new FileInputStream(file.getPath());
} catch (FileNotFoundException e) {
stream=null;
// readable file in filesystem
try {
stream=new FileInputStream(file.getPath());
} catch (FileNotFoundException e) {
stream=null;
}
}
}
} else if (uri.toString().contains("content://")) {

if (stream == null) {
// dealing with content provider
// trying to get URI from intent action
try {
// getting a writable file descriptor
Expand Down

1 comment on commit 4f6885b

@VishalNehra
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly fixes #426

Please sign in to comment.