[英]How to open the “**sdcard files in android**” and if i click the file it must open in emulator?
如何打开“ android中的sdcard文件 ”,如果我单击它必须在模拟器中打开的文件?
public class Testopen extends Activity {
private List<String> list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "mymusic" //folder name
);
if (!file.exists()) {
file.mkdirs();
}
ListView lv = (ListView) findViewById(R.id.listview1);
list = getSD();
lv.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
Log.d("LOG", "FIRE !!!! " );
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(Testopen.this,"clicked an item", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Testopen.this, OpenFileActivity.class);
startActivity(intent);
}
});
}
//read from sdcard
private List<String> getSD() {
List<String> item = new ArrayList<String>();
File f = new File("/sdcard/download");
File[] files = f.listFiles();
for(int i = 0; i < files.length; i++) {
File file = files[i];
//take the file name only
long size = file.length()/1024;
String myfile = file.getPath().substring(file.getPath().lastIndexOf("/")+1,file.getPath().length()).toLowerCase();
//item.add(myfile);
item.add(myfile+" "+"Size:"+size+" KB");
// Log.d("LOG", "fs "+file.length());
//item.add(file.length());
}
return item;
}
}
Check out this code - 查看此代码-
Extends ListActivity. 扩展ListActivity。
ArrayAdapter<String> adapter;
int clickCounter=0;
ArrayList<String> listItems=new ArrayList<String>();
private File[] imagelist;
String[] pdflist;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File images = Environment.getExternalStorageDirectory();
imagelist = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for(int i = 0;i<imagelist.length;i++)
{
pdflist[i] = imagelist[i].getName();
}
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
}
@SuppressWarnings("rawtypes")
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && imagelist[(int) id].isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imagelist[(int) id].getAbsoluteFile());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
Main.xml main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />
</LinearLayout>
What you are looking for is something similar to a File Explorer. 您正在寻找的类似于文件资源管理器。 Check out the below link.
查看以下链接。 It might be helpful.
这可能会有所帮助。
http://android-er.blogspot.com/2010/01/implement-simple-file-explorer-in.html http://android-er.blogspot.com/2010/01/implement-simple-file-explorer-in.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.