Skip to content

Commit 5302b47

Browse files
committed
added mime type lookup for files and using intent to open file with specialised apps
1 parent 6c917c0 commit 5302b47

File tree

2 files changed

+387
-1
lines changed

2 files changed

+387
-1
lines changed

app/src/main/java/com/filebrowser/amit_gupta/filebrowser/FileBrowser.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.filebrowser.amit_gupta.filebrowser;
22

3+
import android.content.Intent;
4+
import android.content.pm.PackageManager;
5+
import android.content.pm.ResolveInfo;
6+
import android.net.Uri;
37
import android.os.Bundle;
48
import android.os.Environment;
59
import android.support.design.widget.FloatingActionButton;
@@ -33,6 +37,8 @@ public class FileBrowser extends AppCompatActivity
3337
private int totalSize;
3438
ListView listView;
3539
TextView textView;
40+
private final int PICK_FILE_RESULT_CODE = 999;
41+
private long timeStamp;
3642

3743
@Override
3844
protected void onCreate(Bundle savedInstanceState) {
@@ -99,7 +105,11 @@ public void onBackPressed() {
99105
listView.setAdapter(new ArrayAdapter(this,
100106
android.R.layout.simple_list_item_1, myList));
101107
}else {
102-
Toast.makeText(getApplicationContext(),"AT THE ROOT - no more back", Toast.LENGTH_SHORT).show();
108+
Toast.makeText(getApplicationContext(),"AT THE ROOT Folder, press one more back to Exit App", Toast.LENGTH_SHORT).show();
109+
if(System.currentTimeMillis() - timeStamp < 200){
110+
super.onBackPressed();
111+
}
112+
timeStamp = System.currentTimeMillis();
103113
}
104114
// super.onBackPressed();
105115
}
@@ -176,6 +186,50 @@ public void onItemClick(AdapterView<?> adapterView, View view, int j, long l) {
176186
Toast.makeText(getApplicationContext(),"Folder is Empty", Toast.LENGTH_SHORT).show();
177187
}
178188

189+
}else {
190+
String mimeType = MediaFile.getMimeTypeForFile(temp_file.toString()); //getContentResolver().getType(Uri.parse("file://" + temp_file));
191+
String fileName = temp_file.getName();
192+
int dotposition= fileName.lastIndexOf(".");
193+
String file_Extension = "";
194+
if(dotposition != -1) {
195+
String filename_Without_Ext = fileName.substring(0, dotposition);
196+
file_Extension = fileName.substring(dotposition + 1, fileName.length());
197+
}
198+
199+
Intent intent = new Intent();
200+
intent.setAction(Intent.ACTION_VIEW);
201+
intent.setDataAndType(Uri.parse("file://" + temp_file), mimeType);
202+
ResolveInfo info = getPackageManager().resolveActivity(intent,
203+
PackageManager.MATCH_DEFAULT_ONLY);
204+
if (info != null) {
205+
startActivity(Intent.createChooser(intent, "Complete action using"));
206+
// startActivity(intent);
207+
}
179208
}
180209
}
210+
// void pickFile(File aFile) {
211+
// Intent theIntent = new Intent(Intent.ACTION_PICK);
212+
// theIntent.setData(Uri.fromFile(aFile)); //default file / jump directly to this file/folder
213+
// theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
214+
// theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
215+
// try {
216+
// startActivityForResult(theIntent,PICK_FILE_RESULT_CODE);
217+
// } catch (Exception e) {
218+
// e.printStackTrace();
219+
// }
220+
// }
221+
222+
@Override
223+
protected void onActivityResult(int requestCode, int resultCode, Intent data){
224+
switch (requestCode) {
225+
case PICK_FILE_RESULT_CODE: {
226+
if (resultCode==RESULT_OK && data!=null && data.getData() !=null) {
227+
String theFilePath = data.getData().getPath();
228+
//handle code
229+
}
230+
break;
231+
}
232+
}
233+
}
234+
181235
}

0 commit comments

Comments
 (0)