Android - 从网上下载图片,保存到应用程序私有位置的内部存储器,显示列表项

Kee*_*13r 7 java android inputstream save fileoutputstream

我想要做的是:我希望我的应用程序从Internet下载图像并将其保存到手机的内部存储器中,该位置是应用程序专用的位置.如果列表项没有可用的图像(即无法在Internet上找到),我想要显示默认的占位符图像.这是我在list_item_row.xml文件中定义的默认图像.

在我的ListActivity文件中,我正在调用我编写的CustomCursorAdapter类的实例.它在CustomCursorAdapter中,我遍历所有列表项并定义需要映射到视图的内容,包括尝试从内部存储器读取它的图像文件.

我已经看到了关于这个主题的几个问题,但这些例子要么特定于外部手机内存(例如SDCard),涉及保存字符串而不是图像,要么涉及使用Bitmap.CompressFormat来降低文件的分辨率(这是不必要的)我的情况,因为这些图像将是已经很小分辨率的小缩略图).试图将每个示例中的代码拼凑起来一直很困难,因此我询问了我的具体示例.

目前,我相信我已经编写了有效的代码,但没有显示我的列表项的图像,包括默认的占位符图像.我不知道问题是由无效的下载/保存代码或无效的读取代码引起的 - 我不知道如何检查内部存储器以查看图像是否存在.

无论如何,这是我的代码.任何帮助将不胜感激.

ProductUtils.java

public static String productLookup(String productID, Context c) throws IOException {
    URL url = new URL("http://www.samplewebsite.com/" + productID + ".jpg");
    URLConnection connection = url.openConnection();
    InputStream input = connection.getInputStream();
    FileOutputStream output = 
        c.openFileOutput(productID + "-thumbnail.jpg", Context.MODE_PRIVATE);
    byte[] data = new byte[1024];

    output.write(data);
    output.flush();
    output.close();
    input.close();
}
Run Code Online (Sandbox Code Playgroud)

CustomCursorAdapter.java

public class CustomCursorAdapter extends CursorAdapter {
    public CustomCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);

        String fileName = 
                cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_IMAGE_FILE_PATH));

        Bitmap bMap = BitmapFactory.decodeFile(fileName);
        thumbnail.setImageBitmap(bMap);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.list_item_row, parent, false);
        bindView(v, context, cursor);
        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

dac*_*cwe 7

似乎遗漏了一些代码,我重新写了这样的代码:

ProductUtils.java

public static String productLookup(String productID, Context c) throws IOException {

    URL url = new URL("http://www.samplewebsite.com/" + productID + ".jpg");

    InputStream input = null;
    FileOutputStream output = null;

    try {
        String outputName = productID + "-thumbnail.jpg";

        input = url.openConnection().getInputStream();
        output = c.openFileOutput(outputName, Context.MODE_PRIVATE);

        int read;
        byte[] data = new byte[1024];
        while ((read = input.read(data)) != -1)
            output.write(data, 0, read);

        return outputName;

    } finally {
        if (output != null)
            output.close();
        if (input != null)
            input.close();
    }
}
Run Code Online (Sandbox Code Playgroud)


Kee*_*13r 2

看起来在尝试读取图像时仅仅引用图像文件名是不够的,我必须调用 getFilesDir() 来获取文件存储的路径。下面是我使用的代码:

String path = context.getFilesDir().toString();
String fileName = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_PRODUCT_ID));

if (fileName != null && !fileName.equals("")) {
    Bitmap bMap = BitmapFactory.decodeFile(path + "/" + fileName);
    if (bMap != null) {
        thumbnail.setImageBitmap(bMap);
    }
}
Run Code Online (Sandbox Code Playgroud)