0

this is my file browser code below when show files from sd card i put multiple apk files in sd card but this code just show apk files in sd card not install when click what do ido? please help me. i want to run apk files which show on listview but is not run how do i modify this code to run selected file??

         public class MainActivity extends ListActivity {

 private List<String> item = null;
 private List<String> path = null;
 private String root;
private TextView myPath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myPath = (TextView)findViewById(R.id.path);

    root = Environment.getExternalStorageDirectory().getPath();

    getDir(root);
}

private void getDir(String dirPath)
{
 myPath.setText("Location: " + dirPath);
 item = new ArrayList<String>();
 path = new ArrayList<String>();
 File f = new File(dirPath);
 File[] files = f.listFiles();

 if(!dirPath.equals(root))
 {
  item.add(root);
  path.add(root);
  item.add("../");
  path.add(f.getParent()); 
 }

 for(int i=0; i < files.length; i++)
 {
  File file = files[i];

  if(!file.isHidden() && file.canRead()){
   path.add(file.getPath());
      if(file.isDirectory()){
       item.add(file.getName() + "/");
      }else{
       item.add(file.getName());
      }
  } 
 }

  ArrayAdapter<String> fileList =
   new ArrayAdapter<String>(this, R.layout.row, item);
  setListAdapter(fileList); 
  }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));

 if (file.isDirectory())
 {
 if(file.canRead()){
  getDir(path.get(position));
 }else{
  new AlertDialog.Builder(this)
 .setIcon(R.drawable.ic_launcher)
 .setTitle("[" + file.getName() + "] folder can't be read!")
 .setPositiveButton("OK", null).show(); 
} 
}else {
new AlertDialog.Builder(this)
 .setIcon(R.drawable.ic_launcher)
 .setTitle("[" + file.getName() + "]")
 .setPositiveButton("OK", null).show();

}
}

}

1 Answers1

0

Implement a ListView (follow this TUTORIAL).

Then implement a listener to handle the click on the items ( listView.onItemClickListener(); )

and then use the snipper below for the installation:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall); 

ref: https://stackoverflow.com/a/4604922/847818

You have to go step-by-step.

Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • This code would help you if you know how to use it. Do your homework and google. – Enrichman Jun 14 '13 at 13:07
  • is work just tell me my files is inside mnt/sdcard/apkfile folder but my app always go sd card how do new folder path in default sd card path which is root = Environment.getExternalStorageDirectory().getPath(); – smartguy1200 Jun 14 '13 at 13:15
  • Do you want to get the apkfile folder or create the folder? – Enrichman Jun 14 '13 at 13:30
  • this code work for me thnx. tell me how do i show in listview only that applications icon which i load from specific folder> – smartguy1200 Jun 14 '13 at 13:31
  • That's another question.. you have to try to resolve your problems by yourself. If it's something difficult then ask here, showing what have you tried and what is not working. You're asking me to do everything. – Enrichman Jun 14 '13 at 13:34