1

有一個活動,從可繪製的文件夾中讀取圖像,並在幻燈片放映視圖中顯示它們。下面是我的代碼。現在我想從我的SD卡或內部存儲器中顯示圖像到viewpager。在下面的代碼中應該做些什麼改變才能讀取SD卡或內部存儲器中的圖像,並將它們以幻燈片的形式顯示在viewpager中。從SD卡或內部存儲顯示圖像查看尋呼機android

活動

public class MainActivity extends AppCompatActivity { 
    ViewPager viewPager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     setContentView(R.layout.activity_main); 

     viewPager = (ViewPager) findViewById(R.id.viewPager); 

     ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this); 

     viewPager.setAdapter(viewPagerAdapter); 
     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new MyTimerTask(), 3000,3000); 

    } 
    public class MyTimerTask extends TimerTask { 

     @Override 
     public void run() { 

      MainActivity.this.runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 

        if (viewPager.getCurrentItem() == 0) { 
         viewPager.setCurrentItem(1); 
        } else if (viewPager.getCurrentItem() == 1) { 
         viewPager.setCurrentItem(2); 
        } else if (viewPager.getCurrentItem() == 2) { 
         viewPager.setCurrentItem(3); 
        } else if (viewPager.getCurrentItem() == 3) { 
         viewPager.setCurrentItem(4); 
        } else { 
          viewPager.setCurrentItem(0); 
         } 

        } 


       ; 

      }); 
     } 
    } 
} 

ViewPagerAdapter

public class ViewPagerAdapter extends PagerAdapter { 

    private Context context; 
    private LayoutInflater layoutInflater; 
    private Integer [] images = {R.drawable.index1,R.drawable.index2,R.drawable.index3,R.drawable.index4,R.drawable.index5}; 

    public ViewPagerAdapter(Context context) { 

     this.context = context; 
    } 
    @Override 
    public int getCount() { 

     return images.length; 
    } 
    @Override 
    public boolean isViewFromObject(View view, Object object) { 

     return view == object; 
    } 
    @Override 
    public Object instantiateItem(ViewGroup container, final int position) { 

     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = layoutInflater.inflate(R.layout.custom_layout, null); 
     ImageView imageView = (ImageView) view.findViewById(R.id.imageView); 
     imageView.setImageResource(images[position]); 


     ViewPager vp = (ViewPager) container; 
     vp.addView(view, 0); 
     return view; 
    } 
    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 

     ViewPager vp = (ViewPager) container; 
     View view = (View) object; 
     vp.removeView(view); 

    } 
} 
+1

檢查了這一點:https://stackoverflow.com/a/11237382/5402482 –

+0

太複雜了,林困惑 – emin

+0

@emin,首先需要基本上有一個函數來將圖像加載爲位圖。之後,你可以把它們放在任何你想要的地方。 –

回答

1

您可以使用畢加索從SD卡載入圖像。蒞臨指導:
Picasso Library

你的情況:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); // retrieve the storage dir 
    Picasso.with(context).load(new File(baseDir + File.separator + your_filename)).into(imageView); 
+0

我的代碼的哪一部分應添加此代碼,適配器頁面? – emin

+0

根據你的代碼,我基本上會得到一個名字爲aded的字符串列表,每當我加載一張圖片時,我將它添加到畢加索(your_filename)的路徑中。但你可以隨心所欲地做到這一點。 –

+0

我有我的SD卡中的圖像文件(文件名是圖像),圖像名稱是index1.png,index2.png,..直到index5.png,你能改進我的代碼嗎,請 – emin

相關問題