0

hi i try get file name from external stroge but i have an error where is problem?

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;
}

}

add manifest.xml permissions

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

ddms log message is

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)

my 55 line is

                        for (File f : yourDir.listFiles()) {
Michael
  • 57,169
  • 9
  • 80
  • 125
amra
  • 3
  • 3

1 Answers1

0

The only possible explanation I can see is that listFiles() is returning null.

From the docs:

Returns an array of files contained in the directory represented by this file. The result is null if this file is not a directory. The paths of the files in the array are absolute if the path of this file is absolute, they are relative otherwise.

Returns

an array of files or null.

So, your file is not a directory.

You could try creating your file using only a string parameter to the dir instead

(and skip your "yourpath" parameter):

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

which constructs a new file using the specified path.

keyser
  • 18,829
  • 16
  • 59
  • 101
  • thanks its works but write only one file name where is other?h – amra Aug 24 '13 at 15:14
  • @amra Not sure. Are you 100% positive it's in the directory you're listing? – keyser Aug 24 '13 at 15:15
  • @Keyser I m not sure because i print file only this message shown [Ljava.io.File;@47f42770 – amra Aug 24 '13 at 15:26
  • @amra That looks like the hashcode of a File object. Try using the file's `toString()` or `getName()` method. – keyser Aug 24 '13 at 16:21
  • @Keyser problem is listFiles method only main directory files return,dont under another path in main path – amra Aug 24 '13 at 19:18
  • @amra Did you use my code? If so, you might want to specify another path than `sdCardRoot` like [this](http://stackoverflow.com/a/5196428/645270). There are more examples here on SO if you search for stuff like `list files directory android` – keyser Aug 24 '13 at 19:40