嗨,我尝试从外部 stroge 获取文件名,但我有一个错误哪里有问题?
public class MainActivity extends Activity {
//protected ContextWrapper context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test=(Button) findViewById(R.id.button1);
test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//ContextWrapper context = new ContextWrapper();
// TODO Auto-generated method stub
// File datapath = Environment.getDataDirectory();
//File cacheDir = new File(context.getCacheDir(),"tempfile");
String extState = new String();
extState=Environment.getExternalStorageState();
//File amcuk=Environment.getExternalStorageDirectory();
//String externalname=new String();
//String internalname=new String();
//you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
//if you are only interested in reading the filesystem
//try {
String name=new String();
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
Log.d("ss", "sad");}
else {
//File f=new File(extState);
Log.d("www", "www");
Log.d("wwwaaaaaaaaaaaaaaa", "wwwaaaaaaaaaaaaa");
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
if (f.isFile()){
name = f.getName();
}
// make something with the name
}
}// catch (Exception e) {
// TODO: handle exception
//Log.d("sasd", "sad");*/
//}
Log.d("dosya adları", extState);
//File mydataDir =new File(path,"path");
//Log.d("dosya adları internal", internalname);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
添加 manifest.xml 权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
ddms 日志消息是
08-24 16:54:38.986: E/AndroidRuntime(2629): FATAL EXCEPTION: main
08-24 16:54:38.986: E/AndroidRuntime(2629): java.lang.NullPointerException
08-24 16:54:38.986: E/AndroidRuntime(2629): at com.example.fileexplorear.MainActivity$1.onClick(MainActivity.java:55)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.view.View.performClick(View.java:2461)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.view.View$PerformClick.run(View.java:8890)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.os.Handler.handleCallback(Handler.java:587)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.os.Handler.dispatchMessage(Handler.java:92)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.os.Looper.loop(Looper.java:123)
08-24 16:54:38.986: E/AndroidRuntime(2629): at android.app.ActivityThread.main(ActivityThread.java:4632)
08-24 16:54:38.986: E/AndroidRuntime(2629): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 16:54:38.986: E/AndroidRuntime(2629): at java.lang.reflect.Method.invoke(Method.java:521)
08-24 16:54:38.986: E/AndroidRuntime(2629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-24 16:54:38.986: E/AndroidRuntime(2629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-24 16:54:38.986: E/AndroidRuntime(2629): at dalvik.system.NativeStart.main(Native Method)
我的 55 行是
for (File f : yourDir.listFiles()) {