0

我是 Java 和 Android 开发的新手。我试图从谷歌图像下载 jpg 图像并在网格中显示它们,但 BitmapFactory.decode 总是返回 null。我在 Mac 上工作,但没有成功启动 avd 来检查下载的文件(我无法访问带有 ADT 的设备上的文件层次结构......我猜它只适用于 AVD)。我检查了不下载 CMJN 图像,我检查了文件是否下载成功但没有成功...

这是完整的代码......任何帮助表示赞赏:)

Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.downloadjpgtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="16" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
        <activity
            android:name="com.test.downloadjpgtest.Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

资源/布局/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

com.test.downloadjpgtest/Main.java

package com.test.downloadjpgtest;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Main extends Activity {
GridView gridview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // GetLibraryTask
    DownloadTask downloadTask = new DownloadTask(this);
    downloadTask.execute("http://sphotos-b.xx.fbcdn.net/hphotos-ash4/c0.0.400.400/p403x403/382050_498705510162306_1891776516_n.jpg",
                                                "http://data.alipson.fr/ravensburger.17/ravensburger-puzzle-1000-pieces-panoramique-amitie-entre-animaux-.44481-1.jpg",
                                                "http://a388.idata.over-blog.com/400x400/3/03/14/36/mammiferes/lynx-canada-04.jpg",
                                                "http://photos.ugal.com/6353/43314/205322/246370/vignette-chapeaux-animaux.400.jpg"
                                                );

    // grille des couvertures
    gridview = (GridView) findViewById(R.id.gridview);
}

// called well all downloads finished
public void setGridAdapter(){
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}


public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public Bitmap[] imgs;

    public ImageAdapter(Context c) {
        mContext = c;
        get_images();
    }

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

    public Object getItem(int position) {
        return imgs[position];
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(imgs[position]);
        return imageView;
    }

    private void get_images(){
        File directory = mContext.getDir("jpgfolder", Context.MODE_PRIVATE);
        File[] filesInJPGFolder = directory.listFiles();
        imgs = new Bitmap[filesInJPGFolder.length];

        for (int cpt=0; cpt<filesInJPGFolder.length;cpt++){
            File imgFile = new  File(filesInJPGFolder[cpt].toString());
            imgs[cpt] = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); // imgs[cpt] Always Null !!!!!!!!!
            Log.d("Main", "imgCovers[cpt] " + imgs[cpt]);
        }
    }
}

}

com.test.downloadjpgtest/DownloadTask.java

package com.test.downloadjpgtest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.AsyncTask;
import android.util.Log;


public class DownloadTask extends AsyncTask<String, Void, String> {
private Activity activity;
private static final int files2Download = 4;
private static int filesDownloaded = 0;

public DownloadTask(Activity activity){
    this.activity = activity;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(String... args) {               
    this.saveFileOnDisk(args[0], "1.jpg");
    this.saveFileOnDisk(args[1], "2.jpg");
    this.saveFileOnDisk(args[2], "3.jpg");
    this.saveFileOnDisk(args[3], "4.jpg");
    return "ok"; // just to return a string
}

protected String saveFileOnDisk(String urlString, String outputName){
    try {
        // in
        URL url = new URL(urlString);
        InputStream input = url.openConnection().getInputStream();

        // out
        ContextWrapper contextWrapper = new ContextWrapper(activity.getApplicationContext());
        File directory = contextWrapper.getDir("jpgfolder", Context.MODE_PRIVATE);
        File newFile = new File(directory, outputName);
        newFile.createNewFile();
        FileOutputStream fos = this.activity.getApplicationContext().openFileOutput(outputName, Context.MODE_PRIVATE);
        int read;
        byte[] data = new byte[1024];
        while ((read = input.read(data)) != -1)
            fos.write(data, 0, read);
        fos.close();
        DownloadTask.filesDownloaded++;
        return newFile.getAbsolutePath();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "ko";
}

protected void onProgressUpdate(String... progress) {
    Log.i("progress", progress[0]);
}

@Override
protected void onPostExecute(String result) {
    try {
        Log.d("DownloadTask", "All downloads finished");
        // check downloaded files
                    ContextWrapper contextWrapper = new ContextWrapper(activity.getApplicationContext());
                    File directory = contextWrapper.getDir("jpgfolder", Context.MODE_PRIVATE);
                    File[] filesInDirectory = directory.listFiles();
                    for(int i=0, max = filesInDirectory.length; i < max; i++){
                        Log.i("DownloadTask", "file " + i + " > " + filesInDirectory[i] + " exist?" + filesInDirectory[i].exists());
                    }
        // set grid adapter ... cf Main.java
        ((Main) this.activity).setGridAdapter();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

4

2 回答 2

0

首先,您确定该文件夹jpgfolder存在吗?因为getDir如果不退出该方法就不会创建它。

如果这不是问题,之前有一个答案:BitmapFactory.decodeFile 返回 null 即使存在可以提供帮助的图像。

于 2012-12-18T09:26:35.970 回答
0

只需进行一些更改即可使我工作:

主.java

package com.test.downloadjpgtest;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Main extends Activity {
    GridView gridview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // récupère la GridView
        gridview = (GridView) findViewById(R.id.gridview);

        // GetLibraryTask
        DownloadTask downloadTask = new DownloadTask(this);
        downloadTask.execute("http://sphotos-b.xx.fbcdn.net/hphotos-ash4/c0.0.400.400/p403x403/382050_498705510162306_1891776516_n.jpg",
                             "http://data.alipson.fr/ravensburger.17/ravensburger-puzzle-1000-pieces-panoramique-amitie-entre-animaux-.44481-1.jpg",
                             "http://a388.idata.over-blog.com/400x400/3/03/14/36/mammiferes/lynx-canada-04.jpg",
                             "http://photos.ugal.com/6353/43314/205322/246370/vignette-chapeaux-animaux.400.jpg");
    }

    // called well all downloads finished
    public void setGridAdapter(){
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        public Bitmap[] imgs;

        public ImageAdapter(Context c) {
            mContext = c;
            getImages();
        }

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

        public Object getItem(int position) {
            return imgs[position];
        }

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

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(imgs[position]);
            return imageView;
        }

        private void getImages(){
            ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
            for (File imgfile : DownloadTask.getImages(this.mContext)) {
               Bitmap bmp = BitmapFactory.decodeFile(imgfile.getAbsolutePath());
               if (bmp != null)
                   bitmaps.add(bmp);
             }
             this.imgs = bitmaps.toArray(new Bitmap[bitmaps.size()]);
        }


    }
}

下载Task.java

package com.test.downloadjpgtest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;


public class DownloadTask extends AsyncTask<String, Void, String> {
    private Activity activity;

    public DownloadTask(Activity activity){
        this.activity = activity;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... args) {       
        int idx = 0;
        for (String urlString : args) {     
            String name = Integer.toString(++idx)+".jpg";
            URL url = null;
            try {
                url = new URL(urlString);
            } catch (MalformedURLException ex) {
                Log.e("DownloadTask","MalformedURLException",ex);
            }
            saveFileOnDisk(url, name);
        }
        return "ok"; // just to return a string
    }

    protected String saveFileOnDisk(URL url, String outputName){
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(url.openConnection().getInputStream());
            File directory = DownloadTask.getImageDirectory(this.activity);
            File newFile = new File(directory, outputName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
            int bytesRead;
            byte[] data = new byte[4096];
            while ((bytesRead = bis.read(data)) != -1){
                bos.write(data, 0, bytesRead);
            }
            Log.d("DownloadTask", "writing data bos " + directory.getAbsolutePath() + '/' + outputName);
            bos.flush();
            bos.close();
            return newFile.getAbsolutePath();
        } catch (Exception ex) {
            Log.e("DownloadTask","Failed to download or write image file",ex);
        } finally {
            if (bis != null)
                try { 
                    bis.close(); 
                }
                catch (Exception ex) {
                    Log.e("DownloadTask","Failed to gracefully close input stream",ex);
                }
        }
        return "ko";
    }

    protected void onProgressUpdate(String... progress) {
        Log.i("progress", progress[0]);
    }

    public static File[] getImages(Context ctxt)
    {
        File[] files = getImageDirectory(ctxt).listFiles();
        return files;
    }

    public static File getImageDirectory(Context ctxt)
    {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        //return ctxt.getDir("jpgfolder", Context.MODE_PRIVATE);
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            Log.d("DownloadTask", "All downloads finished");
            // check downloaded files
            File[] filesInDirectory = DownloadTask.getImages(this.activity);
            for(int i=0, max = filesInDirectory.length; i < max; i++){
                Log.i("DownloadTask", "onPostExecute: file " + i + " > " + filesInDirectory[i]);
            }
            // set grid adapter ... cf Main.java
            ((Main) this.activity).setGridAdapter();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
于 2013-01-04T11:30:27.017 回答