2015-06-14 144 views
1

我開發了一個簡單的Android應用程序,它將從 路徑=「存儲/ MicroSD /視頻」,並將它們呈現在列表視圖中,選擇視頻,選定的視頻將被播放。位於Storage/MicroSD/Videos中的視頻將播放。但其他路徑中的視頻不會被抓取。請給出一些想法,如何從所有路徑獲取視頻(內部存儲和外部存儲)。另外,如果我在不同的手機上運行相同的應用程序,則SD卡路徑與Storage/SD Card o/videos不同。 現在是我的問題:我如何獲得File -object以編程方式指向任何設備的默認視頻目錄?在內部存儲和外部SD卡訪問視頻 - Android

主要活動

public class MainActivity extends Activity { 

    private ListView mListView; 
    private List<String> fileNameList; 
    public String path="storage/MicroSD/Videos"; 
    private File file; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    file = Environment.getExternalStorageDirectory(); 
    fileNameList = getFileListfromSDCard(); 
    final ListView listView = (ListView) findViewById(R.id.list); 
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, android.R.id.text1, fileNameList); 
    listView.setAdapter(adapter1); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      int itemPosition = position; 
      String itemValue = (String) listView.getItemAtPosition(position); 
      Intent intent = new Intent(MainActivity.this, com.video.videolibrary.SelectedVideo.class); 
      intent.putExtra("id", id); 
      intent.putExtra("itemValue", itemValue); 
      intent.putExtra("path", path); 
      startActivity(intent); 
     } 

     }); 

    Button button = (Button) findViewById(R.id.btn_Online); 
    // Capture button clicks 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View arg0) { 
      Intent myIntent = new Intent(MainActivity.this,com.video.videolibrary.SelectedVideo.class); 
      startActivity(myIntent); 
     } 
    }); 

    } 

    private List<String> getFileListfromSDCard() { 

    File files = new File(path); 
    FileFilter filter = new FileFilter() { 
     private final List<String> exts = Arrays.asList("mp4","MP4"); 
     @Override 
     public boolean accept(File pathname) { 
      String ext; 
      String path = pathname.getPath(); 
      ext = path.substring(path.lastIndexOf(".") + 1); 
      return exts.contains(ext); 
     } 
    }; 

    final File [] filesFound = files.listFiles(filter); 
    List<String> flLst = new ArrayList<String>(); 
    if (filesFound != null && filesFound.length > 0) { 
     for (File file : filesFound) { 
      flLst.add(file.getName()); 
     } 
    } 
    return flLst; 
    } 

    } 

選定的視頻類

 public class SelectedVideo extends Activity { 
     VideoView view;AudioManager audioManager; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.selected_video); 
     view = (VideoView)findViewById(R.id.videoView1); 
     Bundle bundle = getIntent().getExtras(); 
     String path=bundle.getString("path"); 
     String itemValue = bundle.getString("itemValue"); 
     view.setVideoURI(Uri.parse(path+"/"+itemValue)); 
     VideoView videoView = (VideoView) findViewById(R.id.videoView1); 
     MediaController mediaController = new MediaController(this); 
     mediaController.setAnchorView(videoView); 
     Uri uri = Uri.parse(path+"/"+itemValue); 
     DisplayMetrics dm = new DisplayMetrics(); 
     this.getWindowManager().getDefaultDisplay().getMetrics(dm); 
     int height = dm.heightPixels; 
     int width = dm.widthPixels; 
    videoView.setMinimumWidth(width); 
    videoView.setMinimumHeight(height); 
    videoView.setMediaController(mediaController); 
    videoView.setVideoURI(uri); 
    videoView.requestFocus(); 
    audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    SeekBar volControl = (SeekBar)findViewById(R.id.volbar); 
    volControl.setMax(maxVolume); 
    volControl.setProgress(curVolume); 
    volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

     @Override 
     public void onStopTrackingTouch(SeekBar arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStartTrackingTouch(SeekBar arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 
      // TODO Auto-generated method stub 
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0); 
     } 
     }); 

     Button stopbutton = (Button) findViewById(R.id.btn_stop); 
     stopbutton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      view.pause(); 

      } 
     }); 

     Button playbutton = (Button) findViewById(R.id.btn_play); 
     playbutton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Bundle bundle = getIntent().getExtras(); 
      String itemValue = bundle.getString("itemValue"); 
      Toast.makeText(getApplicationContext(), 
        itemValue, Toast.LENGTH_LONG) 
        .show(); 
      view.start(); 
     } 
     }); 

     Button backbutton = (Button) findViewById(R.id.btn_back); 
     backbutton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      view.stopPlayback(); 
      startActivity(new Intent(SelectedVideo.this, MainActivity.class)); 
      } 
     }); 

     } 
     @Override 
     public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
    } 
    } 

Android清單文件

 <uses-permission android:name="android.permission.INTERNET"/> 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.read_external_storage" /> 
     <application 
     android:allowBackup="true" 
     android:icon="@mipmap/video_library" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     android:configChanges="orientation|keyboardHidden"> 
     <activity 
     android:name="com.video.videolibrary.MainActivity" 
     android:label="@string/app_name"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     <activity 
     android:name=".SelectedVideo"> 
     </activity> 
     </application> 

     </manifest> 

回答

0

首先:不要硬編碼的路徑,你的SD卡。您應該使用getExternalStorageDirectory()來獲得指向此位置的File對象。所以,你的變量files應分配如下:

File files= Environment.getExternalStorageDirectory(Environment.DIRECTORY_MOVIES); 

由於這將返回默認位置,在視頻,accessable用戶將存儲,這其中應包括您的需求。

Android的API,說這個:

注意:不要用這個詞混淆 「外部」 在這裏。該目錄 可以更好地被認爲是媒體/共享存儲。它是一個文件系統,它可以容納相對較大數量的數據,並在所有應用程序間共享(不強制執行權限)。傳統上,這是SD卡的 ,但它也可以作爲 設備中的內置存儲設備實現,該設備與受保護的內部存儲設備不同,並且可以將 作爲文件系統安裝在計算機上。

另一種選擇是使用ContentProvider。使用光標,你可以查詢這一個,並會得到每個索引視頻。這會讓你不必要地顯式搜索視頻。

基本Cursor -query將基本上是這樣的:

Cursor cursor=getContentResolver().query(
    MediaStore.Video.Media,    // The content URI of the table 
    mProjection,       // The columns to return for each row 
    mSelectionClause,     // Selection criteria 
    null,        // replacing placeholders of Selection criterias 
    mSortOrder);       // The sort order for the returned rows 

    while(cursor.moveToNext()){ 
    //work with your data here 
    } 

從Android-的Manifest.xml刪除這條線,這是不必要的:

<uses-permission android:name="android.permission.read_external_storage" /> 
+0

文件文件= Environment.getExternalStoragePublicDirectory( Environment.DIRECTO RY_MOVIES); 命令返回存儲在移動內部存儲器(m_ASUS_Display_Demo.mp4)中的默認視頻和其他視頻(SD卡中的stord以及應用視頻)未列出。 – Visitjaga

+0

@Visitjaga查看編輯 –

0

試試這個:

String path = Environment.getExternalStorageDirectory().toString(); 
    // String path = "Environment.getExternalStorageDirectory().toString()+"/myvideo"; 

    ArrayList<String>alPath=new ArrayList<String>(); 
    ArrayList<String> alName=new ArrayList<String>(); 

    File directory = new File(path); 
    File[] file = directory.listFiles(); 
    for (int i = 0; i < file.length; i++) { 

      alName.add(file[i].getName()); 
      alPath.add(file[i].getAbsolutePath()); 


    ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String> 
    (VideoList.this,android.R.layout.simple_list_item_1,alName); 
    listView.setAdapter(arrayAdapter); 
相關問題