vindaaron
vindaaron

Reputation: 97

null pointer exception in SdcardActivity

< i tried this code to open music files from sdcard but now m getting null pointer exception at openDirectory("/sdcard"); can anyone help me here.. thnx >

public class SdcardActivity extends ListActivity { 
private List<String> item = null; 
private List<String> path = null; 
private String root = "/sdcard"; 
private TextView myPath;



@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Intent intent=getIntent();
    setContentView(R.layout.main);
    myPath = (TextView)findViewById(R.id.path);
    Log.d("Contentprovider-onCreate", "root"+root);
    try{
        Log.d("Contentprovider-onCreate", "root"+root);
        openDirectory("/sdcard");
    }catch (NullPointerException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    }
public void openDirectory(String dirPath)
{
    myPath.setText("Location: " +dirPath);
    Log.d("Contentprovider-getDir", "path" +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];
        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) {

    //String p =(String) path.get(position);
    File file = new File(path.get(position));
    if (file.isDirectory())
    {
        if(file.canRead())
            openDirectory(path.get(position)); 
        else
        {     new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "] folder can't be read!")
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which){
            // TODO Auto-generated method stub
            dialog.dismiss(); 
            } 
        }).show();
        } 
        }


 else 
        { 
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.parse("file://" + file.getPath());
    String fname=file.getName(); 
    if(fname.endsWith(".jpeg")||fname.endsWith("png")||fname.endsWith(".gif"))
    {  
        intent.setDataAndType(uri, "image/*");
        startActivity(intent);
    }
    else if(fname.endsWith(".mp4")||fname.endsWith(".3gp")) 
    {     
        intent.setDataAndType(uri, "video/*");
        startActivity(intent); 
        } 
    else if(fname.endsWith(".mp3"))
    {  
        intent.setDataAndType(uri, "audio/*");  
        startActivity(intent); 
           } 
    else  
         try {  
             EditText tv = (EditText)findViewById(R.id.tn);
             StringBuilder text = new StringBuilder(); 
             BufferedReader br = new BufferedReader(new FileReader(file));  
             String line; 
             while ((line = br.readLine()) != null) {
                 text.append(line);
                 text.append('\n');
                 //Set the text 
                 tv.setText(text); 
                 }
             }//try 
    catch (IOException e) {
        //You'll need to add proper error handling here
        }//catch
    }
   } 
}  

Upvotes: 0

Views: 583

Answers (2)

Sun Junwen
Sun Junwen

Reputation: 1108

Using Uri.fromFile()

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/some-image-file.png")), "image/*");
startActivity(intent);

Upvotes: 0

ferostar
ferostar

Reputation: 7082

Try loading the root folder with

Environment.getExternalStorageDirectory();

Upvotes: 1

Related Questions