Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从网格视图加载图像,并在parse.com调用的所有图像中滑动_Android_Gridview_Parse Platform_Gallery_Swipe - Fatal编程技术网

Android 从网格视图加载图像,并在parse.com调用的所有图像中滑动

Android 从网格视图加载图像,并在parse.com调用的所有图像中滑动,android,gridview,parse-platform,gallery,swipe,Android,Gridview,Parse Platform,Gallery,Swipe,因此,我成功地从parse.com在我的网格视图中加载了图像,但是我在将图像传递到ImageDetail类时遇到了问题,并且我正在尝试将图像加载到ViewPager中,以便我可以根据发送的图像的位置在库中浏览图像 这里有一些代码供参考,我将非常感谢任何帮助或建议,以帮助我更接近我试图实现的目标 这是我的多媒体资料适配器: public class GalleryAdapter extends BaseAdapter { Context context; LayoutInflat

因此,我成功地从parse.com在我的网格视图中加载了图像,但是我在将图像传递到ImageDetail类时遇到了问题,并且我正在尝试将图像加载到ViewPager中,以便我可以根据发送的图像的位置在库中浏览图像

这里有一些代码供参考,我将非常感谢任何帮助或建议,以帮助我更接近我试图实现的目标

这是我的多媒体资料适配器:

public class GalleryAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflate;
    LoadImages loadImages;
    public static List<MyImages> galleryImages;
    private ArrayList<MyImages> imageUrls;


    public GalleryAdapter(Context context, List<MyImages> galleryImages){
        this.context = context;
        this.galleryImages = galleryImages;
        inflate = LayoutInflater.from(context);
        this.imageUrls = new ArrayList<MyImages>();
        this.imageUrls.addAll(galleryImages);

        loadImages = new LoadImages(context);

    }

    public class ViewHolder {
        ImageView picture;
    }

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

    @Override
    public Object getItem(int position) {
        return galleryImages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (view == null){
            holder = new ViewHolder();
            view = inflate.inflate(R.layout.gallery_image, null);
            // find image in gallery_image
            holder.picture = (ImageView) view.findViewById(R.id.picture);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        // load into myGallery gridview
        loadImages.DisplayImages(galleryImages.get(position).getImages(), holder.picture);
        GalleriesFragment.myGallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO pass position of image to ImageDetail view

                Intent viewPic = new Intent(context, ImageDetail.class);

                // pass data
                viewPic.putExtra("pic", galleryImages.get(position).getImages());

                Bundle bundle = new Bundle();
                bundle.putInt("position", position);

                //viewPic.putExtra("position", galleryImages.get(position));

                //ImageDetail.pager.setCurrentItem(galleryImages.get(position));

                context.startActivity(viewPic);
            }


        });

        return view;
    }


}
--我还尝试将intent传递的图像加载到ImageDetail活动的图像视图中,但无法显示图像

我有更多的类/代码可供参考,这些类/代码与从parse.com调用我的图像有关,但我认为我所共享的内容可能已经足够用于当前的上下文

编辑以包含加载我的图像的类

public class LoadImages {

    CacheMemory cacheMem = new CacheMemory();
    CacheFile cacheFile;
    private Map<ImageView, String> myPictures = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());

    ExecutorService executeService;
    // this will display images to UI
    Handler handleImages = new Handler();

    public LoadImages(Context context){
        cacheFile = new CacheFile(context);
        executeService = Executors.newFixedThreadPool(5);
    }

    final int placeholder = R.drawable.famous;

    public void DisplayImages(String url, ImageView pic){
        myPictures.put(pic, url);
        Bitmap bitmap = cacheMem.get(url);
        if (bitmap != null){
            pic.setImageBitmap(bitmap);
        } else {
            queuePic(url, pic);
            pic.setImageResource(placeholder);
        }
    }

    private void queuePic(String url, ImageView pic){
        PicToLoad loadPic = new PicToLoad(url, pic);
        executeService.submit(new PicLoader(loadPic));
    }

    private Bitmap getBitmap(String url){
        File file = cacheFile.getFile(url);
        Bitmap newBitmap = decode(file);
        if (newBitmap != null){
            return newBitmap;
        }

        // download my gallery images from parse.com
        try {
            Bitmap bitmap = null;
            URL imgUrl = new URL(url);
            HttpURLConnection connect = (HttpURLConnection) imgUrl.openConnection();
            connect.setConnectTimeout(20000);
            connect.setReadTimeout(20000);
            connect.setInstanceFollowRedirects(true);

            InputStream input = connect.getInputStream();
            OutputStream output = new FileOutputStream(file);
            Utilities.copy(input, output);
            output.close();
            connect.disconnect();
            bitmap = decode(file);
            return bitmap;
        } catch (Throwable exception){
            exception.printStackTrace();
            if (exception instanceof OutOfMemoryError)
                cacheMem.clear();
            return null;
        }
    }

    // decode image, scale to reduce memory usage
    private Bitmap decode(File file) {
        try{
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            FileInputStream stream = new FileInputStream(file);
            BitmapFactory.decodeStream(stream, null, options);
            stream.close();

            final int desiredImgSize = 150;
            int width = options.outWidth, height = options.outHeight;
            int scale = 1;
            while (true){
                if (width / 2 < desiredImgSize || height / 2 < desiredImgSize)
                    break;
                    width /= 2;
                    height /= 2;
                    scale *= 2;

            }

            BitmapFactory.Options options2 = new BitmapFactory.Options();
            options2.inSampleSize = scale;
            FileInputStream stream2 = new FileInputStream(file);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, options2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e){

        } catch (IOException e){
            e.printStackTrace();
        }

        return null;
    }

    // for queue
    private class PicToLoad {
        public String url;
        public ImageView pic;

        public PicToLoad(String url2, ImageView pic2){
            url = url2;
            pic = pic2;
        }
    }

    class PicLoader implements Runnable {
        PicToLoad picToLoad;

        PicLoader(PicToLoad picToLoad) {
            this.picToLoad = picToLoad;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                if (recycleImageView(picToLoad))
                    return;
                Bitmap bit = getBitmap(picToLoad.url);
                cacheMem.put(picToLoad.url, bit);
                if (recycleImageView(picToLoad))
                    return;
                DisplayBitmap display = new DisplayBitmap(bit, picToLoad);
                handleImages.post(display);
            } catch (Throwable e){
                e.printStackTrace();
            }
        }   

    }

     boolean recycleImageView(PicToLoad picToLoad) {
            String tag = myPictures.get(picToLoad.pic);
            if (tag == null || !tag.equals(picToLoad.url))
                return true;
            return false;
        }

    // display Bitmap on UI
    class DisplayBitmap implements Runnable {
        Bitmap bitmap;
        PicToLoad picToLoad;

        public DisplayBitmap(Bitmap bm, PicToLoad pl){
            bitmap = bm;
            picToLoad = pl;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (recycleImageView(picToLoad))
                return;
            if (bitmap != null){
                picToLoad.pic.setImageBitmap(bitmap);
            } else {
                picToLoad.pic.setImageResource(placeholder);
            }

        }
    }

    public void clearCache(){
        cacheMem.clear();
        cacheFile.clear();
    }

}
公共类LoadImages{
CacheMemory cacheMem=新的CacheMemory();
缓存文件缓存文件;
私有地图myPictures=Collections.synchronizedMap(新的WeakHashMap());
executeService executeService;
//这将向用户界面显示图像
Handler handleImages=新的Handler();
公共加载图像(上下文){
cacheFile=新的cacheFile(上下文);
executeService=Executors.newFixedThreadPool(5);
}
最终整型占位符=R.drawable.名人;
public void DisplayImages(字符串url,ImageView图片){
myPictures.put(pic,url);
位图Bitmap=cacheMem.get(url);
if(位图!=null){
pic.setImageBitmap(位图);
}否则{
queuePic(url,pic);
pic.setImageResource(占位符);
}
}
私有void queuePic(字符串url,ImageView pic){
PicToLoad loadPic=新的PicToLoad(url,pic);
提交(新的PicLoader(loadPic));
}
私有位图getBitmap(字符串url){
File File=cacheFile.getFile(url);
位图newBitmap=解码(文件);
if(newBitmap!=null){
返回newBitmap;
}
//从parse.com下载我的画廊图片
试一试{
位图=空;
URL imgUrl=新URL(URL);
HttpURLConnection connect=(HttpURLConnection)imgUrl.openConnection();
connect.setConnectTimeout(20000);
connect.setReadTimeout(20000);
connect.setInstanceFlowRedirects(true);
InputStream输入=connect.getInputStream();
OutputStream output=新文件OutputStream(文件);
实用程序。复制(输入、输出);
output.close();
连接。断开();
位图=解码(文件);
返回位图;
}捕获(可丢弃异常){
异常。printStackTrace();
if(OutOfMemoryError的异常实例)
cacheMem.clear();
返回null;
}
}
//解码图像,缩放以减少内存使用
专用位图解码(文件){
试一试{
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
FileInputStream=新的FileInputStream(文件);
解码流(流,空,选项);
stream.close();
最终int desiredImgSize=150;
int width=options.outWidth,height=options.outHeight;
int标度=1;
while(true){
if(宽度/2public class LoadImages {

    CacheMemory cacheMem = new CacheMemory();
    CacheFile cacheFile;
    private Map<ImageView, String> myPictures = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());

    ExecutorService executeService;
    // this will display images to UI
    Handler handleImages = new Handler();

    public LoadImages(Context context){
        cacheFile = new CacheFile(context);
        executeService = Executors.newFixedThreadPool(5);
    }

    final int placeholder = R.drawable.famous;

    public void DisplayImages(String url, ImageView pic){
        myPictures.put(pic, url);
        Bitmap bitmap = cacheMem.get(url);
        if (bitmap != null){
            pic.setImageBitmap(bitmap);
        } else {
            queuePic(url, pic);
            pic.setImageResource(placeholder);
        }
    }

    private void queuePic(String url, ImageView pic){
        PicToLoad loadPic = new PicToLoad(url, pic);
        executeService.submit(new PicLoader(loadPic));
    }

    private Bitmap getBitmap(String url){
        File file = cacheFile.getFile(url);
        Bitmap newBitmap = decode(file);
        if (newBitmap != null){
            return newBitmap;
        }

        // download my gallery images from parse.com
        try {
            Bitmap bitmap = null;
            URL imgUrl = new URL(url);
            HttpURLConnection connect = (HttpURLConnection) imgUrl.openConnection();
            connect.setConnectTimeout(20000);
            connect.setReadTimeout(20000);
            connect.setInstanceFollowRedirects(true);

            InputStream input = connect.getInputStream();
            OutputStream output = new FileOutputStream(file);
            Utilities.copy(input, output);
            output.close();
            connect.disconnect();
            bitmap = decode(file);
            return bitmap;
        } catch (Throwable exception){
            exception.printStackTrace();
            if (exception instanceof OutOfMemoryError)
                cacheMem.clear();
            return null;
        }
    }

    // decode image, scale to reduce memory usage
    private Bitmap decode(File file) {
        try{
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            FileInputStream stream = new FileInputStream(file);
            BitmapFactory.decodeStream(stream, null, options);
            stream.close();

            final int desiredImgSize = 150;
            int width = options.outWidth, height = options.outHeight;
            int scale = 1;
            while (true){
                if (width / 2 < desiredImgSize || height / 2 < desiredImgSize)
                    break;
                    width /= 2;
                    height /= 2;
                    scale *= 2;

            }

            BitmapFactory.Options options2 = new BitmapFactory.Options();
            options2.inSampleSize = scale;
            FileInputStream stream2 = new FileInputStream(file);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, options2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e){

        } catch (IOException e){
            e.printStackTrace();
        }

        return null;
    }

    // for queue
    private class PicToLoad {
        public String url;
        public ImageView pic;

        public PicToLoad(String url2, ImageView pic2){
            url = url2;
            pic = pic2;
        }
    }

    class PicLoader implements Runnable {
        PicToLoad picToLoad;

        PicLoader(PicToLoad picToLoad) {
            this.picToLoad = picToLoad;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                if (recycleImageView(picToLoad))
                    return;
                Bitmap bit = getBitmap(picToLoad.url);
                cacheMem.put(picToLoad.url, bit);
                if (recycleImageView(picToLoad))
                    return;
                DisplayBitmap display = new DisplayBitmap(bit, picToLoad);
                handleImages.post(display);
            } catch (Throwable e){
                e.printStackTrace();
            }
        }   

    }

     boolean recycleImageView(PicToLoad picToLoad) {
            String tag = myPictures.get(picToLoad.pic);
            if (tag == null || !tag.equals(picToLoad.url))
                return true;
            return false;
        }

    // display Bitmap on UI
    class DisplayBitmap implements Runnable {
        Bitmap bitmap;
        PicToLoad picToLoad;

        public DisplayBitmap(Bitmap bm, PicToLoad pl){
            bitmap = bm;
            picToLoad = pl;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (recycleImageView(picToLoad))
                return;
            if (bitmap != null){
                picToLoad.pic.setImageBitmap(bitmap);
            } else {
                picToLoad.pic.setImageResource(placeholder);
            }

        }
    }

    public void clearCache(){
        cacheMem.clear();
        cacheFile.clear();
    }

}
GalleriesFragment.myGallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO pass position of image to ImageDetail view

            Intent viewPic = new Intent(context, ImageDetail.class);

            // pass data
            viewPic.putExtra("pic", galleryImages.get(position).getImages());
            // pass position
            viewPic.putExtra("id", position);

            context.startActivity(viewPic);
        }


    });
public class ImageDetail extends Activity {
    // changed from private to public static
    public static ViewPager pager;
    public static String pic;
    public static int position;
    LoadImages loading = new LoadImages(this);
    ImageView selectedImage;
    ImageView exitToGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_detail);

        // get intent from GalleriesFragment - get selected image
        Intent getPic = getIntent();
        // get image
        pic = getPic.getStringExtra("pic");
        // get position to open image tapped and display in the pager
        position = getPic.getExtras().getInt("id");

        Log.d("position = ", +position+"");

        pager = (ViewPager) findViewById(R.id.pager);
        SwipeImageAdapter swipeAdapter = new SwipeImageAdapter();
        pager.setAdapter(swipeAdapter);
        pager.setCurrentItem(position);


        Toast.makeText(MainActivity.context,
                "Simply hit the back key to return to Galleries.",
                Toast.LENGTH_LONG).show();

    }

    // for closing the view
    @Override
    public void finish() {

        super.finish();
    }

    // Class for Swipe Image Functionality using ViewPager
    private class SwipeImageAdapter extends PagerAdapter {

        public int getCount() {

            // TODO Auto-generated method stub
            return GalleryAdapter.imageUrls.size();

        }

        Object getItem(int position){

            return position;
        }


        @Override
        public boolean isViewFromObject(View view, Object obj) {
            // TODO Auto-generated method stub
            return view == ((ImageView) obj);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = ImageDetail.this;

            selectedImage = new ImageView(context);
            exitToGallery = new ImageView(context);
            exitToGallery.setImageResource(R.drawable.close);

            LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
                    300, 500);
            selectedImage.setLayoutParams(layout);

            String img = GalleryAdapter.galleryImages.get(position).getImages();

            loading.DisplayImages(img, selectedImage);

            selectedImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            exitToGallery.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO return to Galleries
                    finish();

                }

            });

            ((ViewPager) container).addView(selectedImage, 0);
            ((ViewPager) container).addView(exitToGallery, 0);

            return selectedImage;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object obj) {
            ((ViewPager) container).removeView((ImageView) obj);
        }

    }
}