Android简单位图阅读

时间:2014-09-08 00:40:40

标签: java android json image bitmap

我试图从Web服务器读取JSON文件,并将它们转换为列表视图的位图图像。问题是我试过这个例子,但试图创建一个文件系统目录:"resolveUri failed on bad bitmap uri" when putting image on ListView;这可能是一个不正确的路径/技术?我收到一个malformedURI Execption,当我尝试使用常规JSON字符串作为图像时,我收到badURI Exception。关于如何正确解码JSON提要的任何想法将不胜感激。如果您想查看个人资料图片标记,则JSON Feed位于此处:http://shipstudent.com/complaint_desk/androidcentralwall.php。如果您需要更多信息,请与我们联系。

public void updateJSONdata() {


            mCommentList = new ArrayList<HashMap<String, String>>();

            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

            try {

                allPosts = json.getJSONArray(TAG_POSTS);

                // looping through all posts according to the json object returned
                for (int i = 0; i < allPosts.length(); i++) {
                    JSONObject c = allPosts.getJSONObject(i);

                    // gets the content of each tag
                    String title = c.getString(TAG_TITLE);
                    String content = c.getString(TAG_MESSAGE);
                    String username = c.getString(TAG_USERNAME);
                    File sdCard = Environment.getExternalStorageDirectory();
                    String profile_picture = c.getString(loadImageFromWebOperations(TAG_PROFILE_PICTURE,sdCard.getAbsolutePath() + "/dir1/dir2"));
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_TITLE, title);
                    map.put(TAG_MESSAGE, content);
                    map.put(TAG_USERNAME, username);
                    map.put(TAG_PROFILE_PICTURE, profile_picture);

                    // adding HashList to ArrayList
                    mCommentList.add(map);

                }

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

        @SuppressWarnings("resource")
        public static String loadImageFromWebOperations(String url, String path) {
            try {
                InputStream is = (InputStream) new URL(url).getContent();

                System.out.println(path);
                File f = new File(path);

                f.createNewFile();
                FileOutputStream fos = new FileOutputStream(f);
                try {

                    byte[] b = new byte[100];
                    int l = 0;
                    while ((l = is.read(b)) != -1)
                        fos.write(b, 0, l);

                } catch (Exception e) {

                }

                return f.getAbsolutePath();
            } catch (Exception e) {
                System.out.println("Exc=" + e);
                return null;

            }
        }

Logcat如果不使用Bitmap / File解码方法有帮助:

09-07 20:43:08.877: I/Adreno-EGL(1497): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.00.007.018_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
09-07 20:43:08.877: I/Adreno-EGL(1497): OpenGL ES Shader Compiler Version: 17.01.10.SPL
09-07 20:43:10.558: W/System.err(1497):     at com.rynovation.kline.mainViewController.updateJSONdata(mainViewController.java:126)
09-07 20:43:10.558: W/System.err(1497):     at com.rynovation.kline.mainViewController$LoadComments.doInBackground(mainViewController.java:225)
09-07 20:43:10.558: W/System.err(1497):     at com.rynovation.kline.mainViewController$LoadComments.doInBackground(mainViewController.java:1)
09-07 20:44:12.034: I/Adreno-EGL(2592): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.00.007.018_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
09-07 20:44:12.034: I/Adreno-EGL(2592): OpenGL ES Shader Compiler Version: 17.01.10.SPL
09-07 20:44:13.355: E/BitmapFactory(2592): Unable to decode stream: java.io.FileNotFoundException: /https:/shipstudent.com/complaint_desk/appphotos/owl.jpeg: open failed: ENOENT (No such file or directory)
09-07 20:44:13.355: I/System.out(2592): resolveUri failed on bad bitmap uri: https://shipstudent.com/complaint_desk/appphotos/owl.jpeg
09-07 20:44:13.425: E/BitmapFactory(2592): Unable to decode stream: java.io.FileNotFoundException: /https:/shipstudent.com/complaint_desk/appphotos/owl.jpeg: open failed: ENOENT (No such file or directory)
09-07 20:44:13.425: I/System.out(2592): resolveUri failed on bad bitmap uri: https://shipstudent.com/complaint_desk/appphotos/owl.jpeg
09-07 20:44:13.445: E/BitmapFactory(2592): Unable to decode stream: java.io.FileNotFoundException: /https:/shipstudent.com/animal/appphotos/978321177.jpg: open failed: ENOENT (No such file or directory)
09-07 20:44:13.445: I/System.out(2592): resolveUri failed on bad bitmap uri: https://shipstudent.com/animal/appphotos/978321177.jpg
09-07 20:44:13.455: E/BitmapFactory(2592): Unable to decode stream: java.io.FileNotFoundException: /https:/shipstudent.com/animal/appphotos/978321177.jpg: open failed: ENOENT (No such file or directory)
09-07 20:44:13.455: I/System.out(2592): resolveUri failed on bad bitmap uri: https://shipstudent.com/animal/appphotos/978321177.jpg
09-07 20:44:13.475: E/BitmapFactory(2592): Unable to decode stream: java.io.FileNotFoundException: /https:/shipstudent.com/animal/appphotos/978321177.jpg: open failed: ENOENT (No such file or directory)
09-07 20:44:13.475: I/System.out(2592): resolveUri failed on bad bitmap uri: https://shipstudent.com/animal/appphotos/978321177.jpg

如果我尝试以上内容:

String profile_picture = c.getString(loadImageFromWebOperations(TAG_PROFILE_PICTURE.substring(0),sdCard.getAbsolutePath() + "/dir1/dir2"));

我找到了一个未找到协议的logcat:

09-07 21:52:42.585: I/System.out(18987): Exc=java.net.MalformedURLException: Protocol not found: profile_picture
09-07 21:52:42.585: W/System.err(18987): org.json.JSONException: No value for null
09-07 21:52:42.595: W/System.err(18987):    at org.json.JSONObject.get(JSONObject.java:355)
09-07 21:52:42.595: W/System.err(18987):    at org.json.JSONObject.getString(JSONObject.java:515)
09-07 21:52:42.595: W/System.err(18987):    at com.rynovation.kline.mainViewController.updateJSONdata(mainViewController.java:126)
09-07 21:52:42.595: W/System.err(18987):    at com.rynovation.kline.mainViewController$LoadComments.doInBackground(mainViewController.java:228)
09-07 21:52:42.595: W/System.err(18987):    at com.rynovation.kline.mainViewController$LoadComments.doInBackground(mainViewController.java:1)
09-07 21:52:42.595: W/System.err(18987):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-07 21:52:42.595: W/System.err(18987):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-07 21:52:42.595: W/System.err(18987):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-07 21:52:42.595: W/System.err(18987):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-07 21:52:42.595: W/System.err(18987):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-07 21:52:42.595: W/System.err(18987):    at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

我有简单的ImageLoader.class你可以尝试

   public class ImageLoader {

   public static final String TAG = ImageLoader.class.getSimpleName(); 

     private ImageLoaderEngine engine;

    public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,
                         ImageLoadingListener listener) {
    checkConfiguration();
    if (imageAware == null) {
        throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);
    }
    if (listener == null) {
        listener = emptyListener;
    }
    if (options == null) {
        options = configuration.defaultDisplayImageOptions;
    }

    if (TextUtils.isEmpty(uri)) {
        engine.cancelDisplayTaskFor(imageAware);
        listener.onLoadingStarted(uri, imageAware.getWrappedView());
        if (options.shouldShowImageForEmptyUri()) {
            imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));
        } else {
            imageAware.setImageDrawable(null);
        }
        listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);
        return;
    }

    ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());
    String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
    engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);

    listener.onLoadingStarted(uri, imageAware.getWrappedView());
    Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
    if (bmp != null && !bmp.isRecycled()) {
        if (configuration.writeLogs) L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);

        if (options.shouldPostProcess()) {
            ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, engine
                    .getLockForUri(uri));
            ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, options
                    .getHandler());
            if (options.isSyncLoading()) {
                displayTask.run();
            } else {
                engine.submit(displayTask);
            }
        } else {
            bmp = options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);
            listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
        }
    } else {
        if (options.shouldShowImageOnLoading()) {
            imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));
        } else if (options.isResetViewBeforeLoading()) {
            imageAware.setImageDrawable(null);
        }

        ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, engine
                .getLockForUri(uri));
        LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo, options
                .getHandler());
        if (options.isSyncLoading()) {
            displayTask.run();
        } else {
            engine.submit(displayTask);
        }
    }
}

    public void displayImage(String uri, ImageView imageView) {
    displayImage(uri, new ImageViewAware(imageView), null, null);
    }

}

//

public static ImageSize defineTargetSizeForView(ImageAware imageAware, ImageSize maxImageSize) {
    int width = imageAware.getWidth();
    if (width <= 0) width = maxImageSize.getWidth();

    int height = imageAware.getHeight();
    if (height <= 0) height = maxImageSize.getHeight();

    return new ImageSize(width, height);
}

在getView

上的适配器类列表视图中调用此meithod
    ImageLoader.getInstance().displayImage(mData.get(position).getImage0(), holder.photo);