如何从特定文件夹位置的Android外部存储中获取视频文件

时间:2015-03-23 11:30:26

标签: android android-external-storage

当我运行此代码时,我需要为我的项目访问外部视频文件我收到错误消息,显示“不幸的是你的app停止了”。请告诉我我的错误。并告诉我如何做到这一点,让我知道如何处理外部文件存储。

public class MainActivity extends ListActivity 
    {
      private File file;
      private List<String> myList;

     EditText et;
     Button bt;


  public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      bt =  (Button)findViewById(R.id.button1);
      et = (EditText)findViewById(R.id.editText1);
      String txt = et.getText().toString();

      myList = new ArrayList<String>();   



     String root_sd = Environment.getExternalStorageDirectory().toString();
     file = new File( root_sd + "/videos/"+txt ) ;       
     File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
       {
        myList.add( list[i].getName() );
        }

    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myList ));

 }

   protected void onListItemClick(ListView l, View v, int position, long id) 
     {
       super.onListItemClick(l, v, position, id);

      File temp_file = new File( file, myList.get( position ) );  

     if( !temp_file.isFile())        
      {
       file = new File( file, myList.get( position ));
        File list[] = file.listFiles();

         myList.clear();

       for( int i=0; i< list.length; i++)
         {
           myList.add( list[i].getName() );
           }
       Toast.makeText(getApplicationContext(),file.toString(),Toast.LENGTH_LONG).show();               
        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));

       }

     }


   @Override
     public void onBackPressed() {
        String parent = file.getParent().toString();
        file = new File( parent ) ;         
        File list[] = file.listFiles();

        myList.clear();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }
        Toast.makeText(getApplicationContext(), parent,Toast.LENGTH_LONG).show();  
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, myList ));


    }
   }

2 个答案:

答案 0 :(得分:0)

请点击此链接:

http://android-coding.blogspot.in/2011/03/using-videoview-to-play-mp4-from-sdcard.html

并确保提供这两行

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

后进入清单

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
像这样

希望它能起作用..

答案 1 :(得分:0)

您是否在清单文件中包含权限..? 试试这个

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