如何在异步任务中运行图像和视频

时间:2017-01-25 12:10:45

标签: android asynchronous android-asynctask imageview android-videoview

我必须在异步任务中的本地文件夹中运行所有图像和视频视图。我已经从文件夹中检索了所有文件。根据文件类型,我启用ImageViewVideoView的可见性。

我停留在这一点,我必须不断循环,并继续播放所有图像和视频异步,直到应用程序关闭。很少的指导会有所帮助。

我现有的代码

private void SetDirectory()
{
    runOnUiThread(new Runnable() {
        @Override
        public void run() {

            m_FolderPath = Environment.getExternalStorageDirectory().toString();

            File txtDirectory = new File(m_FolderPath + "/TestPlayer/");
            // Create a File object for the parent directory
            boolean IsSuccess = false;
            if(!txtDirectory.exists())
            {
                IsSuccess = txtDirectory.mkdir();
            }
            if(IsSuccess == true)
            {
                Log.d("Folder Created" , "Success");
            }
            else
            {
                Log.d("Folder not Created" , "Not Created");
            }

            List<File> files = loadImageFromStorage(txtDirectory.toString());

            if(files.size() > 0)
            {
                for(int nCount = 0; nCount < files.size() ; nCount ++)
                {
                    String FileName = files.get(nCount).toString();
                    m_CurrentMediaFilePath = FileName;
                    String FileExtension = FileName.substring(FileName.lastIndexOf("."));

                    if(FileExtension.equalsIgnoreCase(".jpg") || FileExtension.equalsIgnoreCase(".png"))
                    {
                        m_VideoView.setVisibility(View.INVISIBLE);
                        m_ImageView.setVisibility(View.VISIBLE);
                        File imgFile = new File(FileName);
                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        m_ImageView.setImageBitmap(myBitmap);
                        m_ImageView.bringToFront();
                        try
                        {
                            Thread.sleep(5000);
                        }
                        catch (Exception ex)
                        {}
                    }
                    else if(FileExtension.equalsIgnoreCase(".mp4"))
                    {
                        try
                        {
                            m_VideoView.setVisibility(View.VISIBLE);
                            m_VideoView.bringToFront();
                            //m_ImageView.setVisibility(View.INVISIBLE);
                            m_VideoView.setVideoPath(FileName);
                            m_VideoView.start();
                        }
                        catch (Exception ex)
                        {
                            Log.d("N", ex.toString());
                        }
                    }
                }
            }

        }
    });
}


public class MediaPlayer extends AsyncTask<String,Void, String>
{
    @Override
    protected void onProgressUpdate(Void... values)
    {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        SetDirectory();
        return "Completed";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
}

1 个答案:

答案 0 :(得分:1)

另一种方法就是这样:

 void showFilesOnUiThread(final List<File> files) {
      new Thread(new Runnable {

          public void run() { 
              for(File file : files) {
                  runOnUiThread(new Runnable() {
                      public void run() {  
                          showFile(file);
                      } 
                  });
                  Thread.sleep(5000);
              }
          }
     }).start();
 }

当然,实施showFile取决于您