<<android编程权威指南(第2版)>>的PhotoGallery项目的练习

来源:互联网 发布:树和图的遍历算法 编辑:程序博客网 时间:2024/05/29 10:36

最近在看<<android编程权威指南(第2版)>>这本书,里面有一个叫PhotoGallery的项目,需要用到Flickr的储存图片功能,

但是Flickr不能访问,所以我修改了FlickrFetchr这个类,用七牛云的图片储存功能(1G以内是免费的)替换Flickr,如果要用这种方式完成这个练习,

首先要申请一个七牛云帐号,然后建立一个解密密钥的后台程序,然后前端向后台请求这个密钥,然后密钥用get的后缀向七牛云服务器提交请求,

接口可以换成和书上一样,这样就完成这个练习了,这是我修改的类

package com.bignerdranch.android.photogallery;import android.net.Uri;import android.util.Log;import android.util.LruCache;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;public class FlickrFetchr {    private static final String TAG = "FlickrFetchr";    private static final String API_KEY = "REPLACE_ME_WITH_A_REAL_KEY";    private LruCache lruCache=Lru.getLruCache();    public byte[] getUrlBytes(String urlSpec) throws IOException {        try {            byte[] value = (byte[]) lruCache.get(urlSpec);            Log.i(TAG, "value=" + value.toString());            return value;        }catch (RuntimeException e) {            URL url = new URL(urlSpec);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            try {                ByteArrayOutputStream out = new ByteArrayOutputStream();                InputStream in = connection.getInputStream();                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {                    throw new IOException(connection.getResponseMessage() +                            ": with " +                            urlSpec);                }                int bytesRead = 0;                byte[] buffer = new byte[1024];                while ((bytesRead = in.read(buffer)) > 0) {                    out.write(buffer, 0, bytesRead);                }                out.close();                lruCache.put(urlSpec,out.toByteArray());                Log.i(TAG, "out.toByteArray=" + out.toByteArray().toString());                return out.toByteArray();            } finally {                connection.disconnect();            }        }    }    public String getUrlString(String urlSpec) throws IOException {        return new String(getUrlBytes(urlSpec));    }    public List<GalleryItem> fetchItems() {        List<GalleryItem> items = new ArrayList<>();        try {            String url = Uri.parse("http://127.0.0.1:8080/a/test/getRecent.do")                    .buildUpon()                    .build().toString();            String jsonString = getUrlString(url);            Log.i(TAG, "Received JSON: " + jsonString);            JSONObject jsonBody = new JSONObject(jsonString);            parseItems(items,jsonBody);        } catch (IOException ioe) {            Log.e(TAG, "Failed to fetch items", ioe);        } catch (JSONException je) {            Log.e(TAG, "Failed to parse JSON", je);        }        return items;    }    private void parseItems(List<GalleryItem> items, JSONObject jsonBody)            throws IOException, JSONException {        JSONObject photosJsonObject = jsonBody.getJSONObject("photos");        JSONArray photoJsonArray = photosJsonObject.getJSONArray("photo");        Log.i(TAG, "jsonarray  "+photoJsonArray);        for (int i = 0; i < photoJsonArray.length(); i++) {            JSONObject photoJsonObject = photoJsonArray.getJSONObject(i);            GalleryItem item = new GalleryItem();            item.setId(photoJsonObject.getString("id"));            item.setCaption(photoJsonObject.getString("title"));            if (!photoJsonObject.has("url_s")) {                continue;            }                        item.setUrl(photoJsonObject.getString("url_s")+"/"+photoJsonObject.getString("title"));            items.add(item);        }    }}                                                                                                                                                              

fetchItems方法的Uri.parse("http://127.0.0.1:8080/a/test/getRecent.do")返回自己的服务器从七牛云服务器取得的图片地址,

具体地址是:https://developer.qiniu.com/kodo/manual/1232/download-process,里面有各种语言的SDK,

我以java为例,写了一个test的类:

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.qiniu.common.Zone;import com.qiniu.storage.BucketManager;import com.qiniu.storage.Configuration;import com.qiniu.storage.UploadManager;import com.qiniu.storage.model.FileInfo;import com.qiniu.util.Auth;@Controller@RequestMapping("/test")public class testController {private static final String accessKey="";private static final String secretKey="";@RequestMapping("/token")public @ResponseBody JSONObject token(){    Configuration configuration=new Configuration();    UploadManager uploadManager = new UploadManager(configuration);        Auth auth = Auth.create(accessKey, secretKey);    String token = auth.uploadToken("wangjiabc");        JSONObject jsonObject=new JSONObject();    jsonObject.put("token", token);        return jsonObject;}@RequestMapping("/getRecent")public @ResponseBody JSONObject getRecent(){Configuration cfg = new Configuration(Zone.zone0());    String bucket = "wangjiabc";    Auth auth = Auth.create(accessKey, secretKey);    BucketManager bucketManager = new BucketManager(auth, cfg);    String prefix = "";    int limit = 1000;    String delimiter = "";    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);         JSONObject jsonObject=new JSONObject();        JSONArray jsonArray=new JSONArray();        int i=0;    while(fileListIterator.hasNext()){    FileInfo[] items=fileListIterator.next();    for(FileInfo item:items){      JSONObject jsonObject2=new JSONObject();      jsonObject2.put("id", item.fsize);      jsonObject2.put("owner", item.hash);      jsonObject2.put("secret", item.hashCode());      jsonObject2.put("server", item.mimeType);      jsonObject2.put("farn", item.type);      jsonObject2.put("title", item.key);      jsonObject2.put("url_s", "http://ory07d5s5.bkt.clouddn.com");      jsonArray.add(jsonObject2);      i++;    }    }        JSONObject jsonObject3=new JSONObject();        jsonObject3.put("page", 1);    jsonObject3.put("pages", 1);    jsonObject3.put("perpage", 1000);    jsonObject3.put("total", i);    jsonObject3.put("photo", jsonArray);        jsonObject.put("photos", jsonObject3);        return jsonObject;}}

accessKey=""和secretKey=""是自己的对象存储的密钥, String bucket = "wangjiabc"是空间的名称,

getRecent返回的jsonObject把七牛云返回的数据处理成了和FlickrFetchr返回的一样的字段,这样就不用修改书里的其它类,

其它的语言也应该对JSON同样的处理,否则就要修改另外的类才能正常运行.



另外这里还有一个token方法,它的主要作用是为android生成上传图片的密钥,为了方便,我也为这个项目加了一个上传图片的类,

import android.app.Application;import android.net.Uri;import android.os.Environment;import android.test.ApplicationTestCase;import android.util.Log;import com.qiniu.android.http.ResponseInfo;import com.qiniu.android.storage.UpCompletionHandler;import com.qiniu.android.storage.UploadManager;import org.json.JSONObject;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import static com.bignerdranch.android.photogallery.FileTypeTest.getFileHexString;/** * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> */public class ApplicationTest extends ApplicationTestCase<Application> {    public ApplicationTest() {        super(Application.class);    }    private UploadManager uploadManager;    private volatile JSONObject responseBody;    // 用于存放sdcard卡上的所有图片路径    public static ArrayList<String> dirAllStrArr = new ArrayList<String>();    public void testPutBytesWithAutoZone() throws Throwable {        //params        Map<String, String> params = new HashMap<String, String>();        params.put("x:foo", "foo");        params.put("x:bar", "bar");        String url = Uri.parse("http://192.168.1.106:8080/a/test/token.do").toString();        String jsonString = new String(getUrlBytes(url));        JSONObject jsonBody = new JSONObject(jsonString);        Log.i("Qiniu.TestPutBytes",jsonBody.getString("token"));         /* 遍历sdcard旗下的所有文件夹开始 */        String sdpath = Environment.getExternalStorageDirectory()                .getAbsolutePath();// 获取sdcard的根路径        Log.i("Qiniu sdpath=",sdpath);        File dirFile = new File(sdpath);        try {            DirAll(dirFile);            Log.i("Qiniu File=",dirAllStrArr.toString());        } catch (Exception e) {            Log.i("Qiniu error=",e.getMessage());        }        final CountDownLatch signal = new CountDownLatch(1);        final String bucket = "wangjiabc";        final String upToken =jsonBody.getString("token");        Iterator<String> iterator=dirAllStrArr.iterator();        while (iterator.hasNext()) {            String  filePath=iterator.next();            File file=new File(filePath);            Log.i("Qiniu PutBytes",file.toString());            File putData =file;            final String expectKey = String.format(filePath, bucket);            //mime type            String mimeType="";            Map<String,String> map=FileTypeTest.getFileType();            Iterator<Map.Entry<String, String>> entryiterator = map.entrySet().iterator();            String filetypeHex = String.valueOf(getFileHexString(file));            while (entryiterator.hasNext()) {                Map.Entry<String,String> entry =  entryiterator.next();                String fileTypeHexValue = entry.getValue();                if (filetypeHex.toUpperCase().startsWith(fileTypeHexValue)) {                    mimeType=entry.getKey();                    break;                }            }            final com.qiniu.android.storage.UploadOptions options = new com.qiniu.android.storage.UploadOptions(params, mimeType, true, null, null);            // final String expectKey = String.format("hello qiniu cloud storage", bucket);            Log.i("Qiniu bucket=", bucket);            Log.i("Qiniu uptoken=", upToken);            Log.i("Qiniu expectkey=", expectKey);            uploadManager = new UploadManager();            uploadManager.put(putData, expectKey, upToken, new UpCompletionHandler() {                public void complete(String key, ResponseInfo info, JSONObject response) {                    Log.i("Qiniu.TestPutBytes", "upload result of bucket " + bucket);                    Log.d("Qiniu.info", info.toString());                    Log.i("Qiniu.response", response.toString());                    responseBody = response;                    signal.countDown();                }            }, options);            signal.await(10, TimeUnit.SECONDS);        }    }    public static void DirAll(File dirFile) throws Exception {        if (dirFile.exists()) {            File[] files = dirFile.listFiles();            for (File file : files) {              //  Log.i("Qiniu file[]",file.toString());                if (file.isDirectory()) {                    String fileName = file.getName();                    // 除sdcard上Android这个文件夹以外。                    if (!fileName.endsWith("Android")) {                        // 如果遇到文件夹则递归调用。                        DirAll(file);                    }                } else {                    // 如果是图片文件压入数组                    String fileName = file.getName();                    if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")                            || fileName.endsWith(".bmp")                            || fileName.endsWith(".gif")                            || fileName.endsWith(".png")) {                        // 如果遇到文件则放入数组                        if (dirFile.getPath().endsWith(File.separator)) {                            dirAllStrArr                                    .add(dirFile.getPath() + file.getName());                        } else {                            dirAllStrArr.add(dirFile.getPath() + File.separator                                    + file.getName());                        }                    }                }            }        }    }    public byte[] getUrlBytes(String urlSpec) throws IOException {        URL url = new URL(urlSpec);        HttpURLConnection connection = (HttpURLConnection)url.openConnection();        try {            ByteArrayOutputStream out = new ByteArrayOutputStream();            InputStream in = connection.getInputStream();            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {                throw new IOException(connection.getResponseMessage() +                        ": with " +                        urlSpec);            }            int bytesRead = 0;            byte[] buffer = new byte[1024];            while ((bytesRead = in.read(buffer)) > 0) {                out.write(buffer, 0, bytesRead);            }            out.close();            return out.toByteArray();        } finally {            connection.disconnect();        }    }}

这个类不用在activity里运行,在studio里点run test运行,它的作用是把android模拟器里所有图片上传到七牛云,

使用前注意添加

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
读取sdcard权限

这个例子的运行效果:



阅读全文
0 0