Android 如何从云存储而不是可绘制的存储中获取图像

Android 如何从云存储而不是可绘制的存储中获取图像,android,Android,我正在创建一个壁纸库应用程序,在其中我从drawable获取图像,并将它们放置在一个阵列中 像这样: static final int[] ICONS = new int[] { R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_brit

我正在创建一个壁纸库应用程序,在其中我从drawable获取图像,并将它们放置在一个阵列中

像这样:

static final int[] ICONS = new int[] 
            { 
            R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb 
            };
但我想从云存储中获取图像,而不是可绘制的图像,并将它们保存在一个数组中

示例:我想从在线存储中获取图像,如:Google Drive、Picasa online、Dropbox、Flicker等

完整代码:

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {


    private GridView photoGrid;
    private int mPhotoSize, mPhotoSpacing;
    private ImageAdapter imageAdapter;

    // Some items to add to the GRID
    private static final String[] CONTENT = new String[] 
            { 
            "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
            "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
            "Micheal Buble" 
            };
    static final int[] ICONS = new int[] 
            { 
            R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb 
            };

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


        // get the photo size and spacing
        mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
        mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

        // initialize image adapter
        imageAdapter = new ImageAdapter();

        photoGrid = (GridView) findViewById(R.id.albumGrid);

        //start sent image to full screen             

        /**
         * On Click event for Single Gridview Item
         * */
        photoGrid.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,  View v,
                    int position,   long id ) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);


            }            
        });
        //end sent image to full screen

        // set image adapter to the GridView
        photoGrid.setAdapter(imageAdapter);

        // get the view tree observer of the grid and set the height and numcols dynamically
        photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });     
    }

    // ///////// ImageAdapter class /////////////////
    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        public int getCount() {
            return CONTENT.length;
        }

        // set numcols
        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        // set photo item height
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            notifyDataSetChanged();
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(final int position, View view, ViewGroup parent) {

            if (view == null)
                view = mInflater.inflate(R.layout.photo_item, null);

            ImageView cover = (ImageView) view.findViewById(R.id.cover);
            TextView title = (TextView) view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (cover.getLayoutParams().height != mItemHeight) {
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(ICONS[position % ICONS.length]);
            title.setText(CONTENT[position % CONTENT.length]);

            return view;
        }
    }

}
package com.td.gridview;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.ViewGroup.LayoutParams;
导入android.view.ViewTreeObserver;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.BaseAdapter;
导入android.widget.GridView;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
私有网格视图光网格;
私人国际摄影展,摄影展;
专用图像适配器;
//要添加到网格中的某些项目
私有静态最终字符串[]内容=新字符串[]
{ 
“阿肯”,“贾斯汀·比伯”,“好吧”,“大肖恩”,
“小甜甜布兰妮”、“希拉里”、“迈克尔·布布尔”、“阿肯”、“贾斯汀·比伯”、“好吧”、“大肖恩”,
“布兰妮·斯皮尔斯”、“希拉里”、“迈克尔·布布尔”、“布兰妮·斯皮尔斯”、“希拉里”、“迈克尔·布布尔”、“阿肯”,
“贾斯汀·比伯”、“好吧”、“大肖恩”、“布兰妮·斯皮尔斯”、“希拉里”、“迈克尔·布布尔”、“阿肯”,
“贾斯汀·比伯”、“好吧”、“大肖恩”、“布兰妮·斯皮尔斯”、“希拉里”、“迈克尔·布布尔”、“阿肯”,
“贾斯汀·比伯”、“好吧”、“大肖恩”、“布兰妮·斯皮尔斯”、“希拉里”、“迈克尔·布布尔”、“布兰妮·斯皮尔斯”,
“Hilary”、“Micheal Buble”、“Akon”、“Justin Bieber”、“OK”、“Big Sean”、“Britney Spears”、“Hilary”,
“Micheal Buble”
};
静态最终整数[]图标=新整数[]
{ 
R.drawable.cover_akon,R.drawable.cover_justin,
R.drawable.cover\u好的,R.drawable.cover\u big\u sean,R.drawable.cover\u britney,R.drawable.cover\u hilary,
R.drawable.cover\u mb,R.drawable.cover\u akon,R.drawable.cover\u justin,R.drawable.cover\u好的,
R.drawable.cover\u big\u sean,R.drawable.cover\u britney,R.drawable.cover\u hilary,R.drawable.cover\u mb,
R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,R.drawable.cover_akon,
R.drawable.cover\u贾斯汀,R.drawable.cover\u好的,R.drawable.cover\u big\u sean,R.drawable.cover\u布兰妮,
R.drawable.cover\u hilary,R.drawable.cover\u mb,R.drawable.cover\u akon,R.drawable.cover\u justin,
R.drawable.cover\u好的,R.drawable.cover\u big\u sean,R.drawable.cover\u britney,R.drawable.cover\u hilary,
R.drawable.cover\u mb,R.drawable.cover\u akon,R.drawable.cover\u justin,R.drawable.cover\u好的,
R.drawable.cover\u big\u sean,R.drawable.cover\u britney,R.drawable.cover\u hilary,R.drawable.cover\u mb,
R.drawable.cover_britney,R.drawable.cover_hilary,R.drawable.cover_mb,R.drawable.cover_akon,
R.drawable.cover\u贾斯汀,R.drawable.cover\u好的,R.drawable.cover\u big\u sean,R.drawable.cover\u布兰妮,
R.drawable.cover\u hilary,R.drawable.cover\u mb
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取照片的大小和间距
mPhotoSize=getResources().getDimensionPixelSize(R.dimen.photo_size);
mPhotoSpacing=getResources().getDimensionPixelSize(R.dimen.photo_间距);
//初始化映像适配器
imageAdapter=新的imageAdapter();
photoGrid=(GridView)findViewById(R.id.albumGrid);
//开始将图像发送到全屏
/**
*单个Gridview项目的单击事件
* */
photoGrid.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图v、,
内部位置,长id){
//正在向FullScreenActivity发送图像id
Intent i=新Intent(getApplicationContext(),SwipeActivity.class);
//传递数组索引
i、 putExtra(“id”,位置);
星触觉(i);
}            
});
//结束发送到全屏的图像
//将图像适配器设置为GridView
photoGrid.setAdapter(imageAdapter);
//获取栅格的视图树观察者,并动态设置高度和numcols
photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(新的ViewTreeObserver.OnGlobalLayoutListener()){
@凌驾
公共图书馆{
if(imageAdapter.getNumColumns()==0){
最终int numColumns=(int)Math.floor(photoGrid.getWidth()/(mPhotoSize+mPhotoSpacing));
如果(numColumns>0){
final int columnWidth=(photoGrid.getWidth()/numColumns)-mPhotoSpacing;
imageAdapter.setNumColumns(numColumns);
imageAdapter.setItemHeight(columnWidth);
}
}
}
});     
}
/////ImageAdapter类/////////////////
公共类ImageAdapter扩展了BaseAdapter{
私人停车场;
私有int-mItemHeight=0;
私有int mNumColumns=0;
private RelativeLayout.LayoutParams mImageViewLayoutParams;
公共映像适配器(){
mInflater=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
mImageViewLayoutParams=新的RelativeLayout.LayoutParams(LayoutP
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);