0

嗨,我尝试从外部 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()) {
4

1 回答 1

0

我能看到的唯一可能的解释listFiles()是返回 null。

文档

返回此文件表示的目录中包含的文件数组。如果此文件不是目录,则结果为null 。如果该文件的路径是绝对的,则数组中文件的路径是绝对的,否则它们是相对的。

退货

文件数组或 null。

因此,您的文件不是目录。

您可以尝试仅使用 dir 的字符串参数来创建文件

(并跳过你的"yourpath"参数):

String sdCardRoot = Environment.getExternalStorageDirectory().toString();
    File yourDir = new File( root_sd ) ;       
    File list[] = yourDir.listFiles();

使用指定的路径构造一个新文件

于 2013-08-24T14:29:29.333 回答