异步将图像加载到ViewPager中

时间:2015-07-05 15:58:32

标签: java android android-asynctask android-viewpager dropbox

我尝试使用ViewPager进行幻灯片放映。我想访问dropbox下载图像,然后将它们加载到片段中。我在片段内部使用异步任务来获取图像,然后在onPostExecute中将图像加载到imageView中。 问题是图像没有显示出来。我想AsyncTask无法更新片段内容,或者当我返回rootView时,它不再更新。 你能告诉我任何解决方案吗?我感谢您的帮助!

ScreenSlideShowActivity2

public class SlideShowActivity2 extends FragmentActivity {

private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
List<DropboxAPI.Entry> content;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slide_show_activity2);
    content = AlbumAdapterDropbox.choosenAlbum.getContent();

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);

}

@Override
public void onBackPressed() {
    if (mPager.getCurrentItem() == 0) {

        super.onBackPressed();
    } else {

        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}


private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", content.get(position).path);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        return content.size();
    }
}

ScreenSlidePageFragment

public class ScreenSlidePageFragment extends Fragment {
ImageView imageView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    String url = bundle.getString("url");
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page, container, false);

    imageView = (ImageView) rootView.findViewById(R.id.contentImageView);
    Picasso.with(getActivity().getApplicationContext()).load(R.drawable.img2).into(imageView);
    File file = new File(getActivity().getCacheDir().getAbsolutePath() + "/" + url);
    String p;
    if (!file.exists()) {
        DownloadImage task = new DownloadImage();
        task.execute(url);
        try {
            p = task.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    } else {
        Picasso.with(getActivity().getApplicationContext()).load(file).into(imageView);

    }
    return rootView;
}

private class DownloadImage extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        android.os.Debug.waitForDebugger();
        String cachePath = null;
        try {
            DropboxAPI.Entry e = MainActivity.mApi.metadata(strings[0], 1, null, false, null);
            FileOutputStream mFos;

            if (e.isDir || e.contents == null) {
                return null;
            }

            cachePath = getActivity().getCacheDir().getAbsolutePath() + "/" + e.fileName();

            try {
                mFos = new FileOutputStream(cachePath);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
                return null;
            }
            MainActivity.mApi.getFile(e.path, null, mFos, null);
            Log.i(DownloadImageTask.class.getName(), e.fileName());


        } catch (DropboxException e) {
            e.printStackTrace();
        }
        return cachePath;
    }

    @Override
    protected void onPostExecute(String string) {

        Picasso.with(getActivity().getApplicationContext()).load(string).into(imageView);
        super.onPostExecute(string);
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您使用picassa,则不需要再次使用asynctask。 Picassa将照顾它。这是两个链接: http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149

http://themakeinfo.com/2015/04/android-retrofit-images-tutorial/

在if语句中,您可以执行以下操作:

Picasso.with(context).load("your url stirng").into(imageView);

删除Asynctask部分。你不再需要了。如果它有帮助我会很高兴。我不是那么专家。我希望有专家的人能给出最好的解决方案。