Android的 - 从下载Web图像,保存到内部存储器中的位置私下的应用程序,用于显示列表项应用程序、器中、图像、位置

2023-09-05 03:52:18 作者:小二来碗汨流满面

我试图做的是这样的:我想我的应用程序从互联网上下载的图像并保存到手机的内部存储在一个位置,是私有的应用程序。如果没有可用于列表项(即它不能在互联网上找到)的形象,我需要有一个默认的占位符图像显示。这是我在我的list_item_row.xml文件作为默认已定义的图像。

在我的ListActivity文件,我打电话我已经写了CustomCursorAdapter类的一个实例。它是在CustomCursorAdapter在那里,我通过所有的列表项迭代和定义的内容需要映射到的意见,包括图像文件,试图从内部存储器读什么。

我已经看到了关于这个问题的几个问题,但无论是例子是针对外部手机内存(如SD卡),涉及节能,而不是图片的字符串,或涉及使用Bitmap.Com pressFormat降低的分辨率该文件(这是不必要的在我的情况,因为这些图像将是已经小分辨率的小缩略图)。试图拼凑code每个例子是困难的,所以我问我的具体例子。

目前,我相信我已经写了有效的code,但没有图像显示的是我的列表项,包括默认的占位符图像。我不知道这个问题可能出自无效的下载/保存code或无效读code - 它并没有帮助,我不知道如何检查内存,如果图像看存在。

不管怎么说,这是我的code。任何帮助将大大AP preciated。

ProductUtils.java

 公共静态字符串productLookup(字符串的productID,语境三)抛出IOException异常{
    网址URL =新的URL(http://www.samplewebsite.com/+ +的productID.JPG);
    URLConnection的连接= url.openConnection();
    输入的InputStream = connection.getInputStream();
    的FileOutputStream输出=
        c.openFileOutput(+的productID-thumbnail.jpg,Context.MODE_PRIVATE);
    byte []的数据=新的字节[1024];

    output.write(数据);
    output.flush();
    output.close();
    input.close();
}
 

CustomCursorAdapter.java

 公共类CustomCursorAdapter扩展的CursorAdapter {
    公共CustomCursorAdapter(上下文的背景下,光标C){
        超(背景下,C);
    }

    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){
        ImageView的缩略图=(ImageView的)view.findViewById(R.id.thumbnail);

        字符串文件名=
                cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_IMAGE_FILE_PATH));

        位图BMAP = BitmapFactory.de codeFILE(文件名);
        thumbnail.setImageBitmap(BMAP);
    }

    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
        LayoutInflater充气= LayoutInflater.from(上下文);
        视图V = inflater.inflate(R.layout.list_item_row,父母,假);
        bindView(ⅴ,上下文,光标);
        返回伏;
    }
}
 
ps中把图片储存为web所有格式的问题

解决方案

貌似简单地指的是图像文件名时试图读取它是不够的,我只好打电话给getFilesDir()来获取文件的路径存储。下面是code我用:

 字符串路径= context.getFilesDir()的toString()。
字符串文件名= cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_PRODUCT_ID));

如果(文件名= NULL和放大器;!&安培;!fileName.equals()){
    位图BMAP = BitmapFactory.de codeFILE(路径+/+文件名);
    如果(BMAP!= NULL){
        thumbnail.setImageBitmap(BMAP);
    }
}
 

What I'm trying to do is this: I want my application to download an image from the Internet and save it to the phone's internal memory in a location that is private to the application. If there is no image available for the list item (i.e. it can't be found on the Internet), I want a default placeholder image to display. This is the image that I have defined in my list_item_row.xml file as the default.

In my ListActivity file, I am calling an instance of a CustomCursorAdapter class I have written. It is in CustomCursorAdapter where I am iterating through all the list items and defining what content needs to be mapped to the views, including the image file by trying to read it from internal memory.

I've seen several questions on this subject, but the examples either are specific to external phone memory (e.g. SDCard), involve saving strings instead of images, or involve using Bitmap.CompressFormat to reduce the resolution of the file (which is unnecessary in my case, as these images will be small thumbnails of already-small resolution). Trying to piece together code from each example has been difficult, hence my asking about my specific example.

At the moment, I believe I've written valid code, but no image is displaying for my list items, including the default placeholder image. I don't know if the problem is being caused by invalid download/save code, or invalid read code - it doesn't help that I don't know how to check internal memory to see if the image exists.

Anyways, here's my code. Any help would be greatly appreciated.

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();
}

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;
    }
}

解决方案

Looks like simply referring to the image file name when trying to read it was not enough, and I had to call getFilesDir() to get the path of the file storage. Below is the code I used:

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);
    }
}