在Android上本地存储来自互联网的图像

时间:2012-06-20 10:33:24

标签: java android caching bitmap httpurlconnection

我需要在Android设备上本地存储一些我从互联网上获取的图像,以便以后更快地显示

我写了这段代码,我没有得到任何异常,但是当我尝试重新加载图片时,我得到一个FileNotFoundException

public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

public boolean saveImage() {
    fileName = this.md5(fullURL);
    URL ulrn = new URL(fullURL);
    HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
    InputStream is = con.getInputStream();
    Bitmap bmp = BitmapFactory.decodeStream(is);
    File f = new File(cacheImagePath, filename); // cacheImagePath is
                                                    // /data/data/com.mycompany.myapp/cache/
    try {
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.JPEG, 92, out);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        Log.e("Exception", "saveImage " + filename);
        e.printStackTrace();
        return false;
    }
}

编辑:

尝试访问文件时出现

LogCat错误:

06-20 14:39:11.965: W/System.err(560): java.io.FileNotFoundException: /data/data/com.accessdev.tellmeplus/cache/files/tagpromo-d3d908befad2892c35f3ba957d5c18 (No such file or directory)
06-20 14:39:11.965: W/System.err(560):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
06-20 14:39:11.965: W/System.err(560):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
06-20 14:39:11.965: W/System.err(560):  at java.io.FileInputStream.<init>(FileInputStream.java:80)
06-20 14:39:11.965: W/System.err(560):  at java.io.FileInputStream.<init>(FileInputStream.java:132)
06-20 14:39:11.965: W/System.err(560):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:362)
06-20 14:39:11.965: W/System.err(560):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:412)
06-20 14:39:11.965: W/System.err(560):  at com.accessdev.tellmeplus.TMPOfferImageAdapter.createBitmap(TMPOfferImageAdapter.java:124)
06-20 14:39:11.970: W/System.err(560):  at pl.polidea.coverflow.AbstractCoverFlowImageAdapter.getItem(AbstractCoverFlowImageAdapter.java:70)
06-20 14:39:11.970: W/System.err(560):  at pl.polidea.coverflow.AbstractCoverFlowImageAdapter.getView(AbstractCoverFlowImageAdapter.java:111)
06-20 14:39:11.970: W/System.err(560):  at pl.polidea.coverflow.AbstractCoverFlowImageAdapter.getView(AbstractCoverFlowImageAdapter.java:1)
06-20 14:39:11.970: W/System.err(560):  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
06-20 14:39:11.970: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.970: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.970: W/System.err(560):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
06-20 14:39:11.970: W/System.err(560):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
06-20 14:39:11.970: W/System.err(560):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-20 14:39:11.970: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.975: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.975: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.975: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.975: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.975: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.975: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.975: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.975: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.975: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.975: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.975: W/System.err(560):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
06-20 14:39:11.975: W/System.err(560):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
06-20 14:39:11.975: W/System.err(560):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-20 14:39:11.980: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.980: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.980: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.980: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.980: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.980: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.980: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.980: W/System.err(560):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
06-20 14:39:11.980: W/System.err(560):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
06-20 14:39:11.985: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.985: W/System.err(560):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
06-20 14:39:11.985: W/System.err(560):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
06-20 14:39:11.985: W/System.err(560):  at android.view.View.measure(View.java:8366)
06-20 14:39:11.985: W/System.err(560):  at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
06-20 14:39:11.985: W/System.err(560):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
06-20 14:39:11.985: W/System.err(560):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 14:39:11.985: W/System.err(560):  at android.os.Looper.loop(Looper.java:130)
06-20 14:39:11.990: W/System.err(560):  at android.app.ActivityThread.main(ActivityThread.java:3691)
06-20 14:39:11.990: W/System.err(560):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 14:39:11.990: W/System.err(560):  at java.lang.reflect.Method.invoke(Method.java:507)
06-20 14:39:11.990: W/System.err(560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
06-20 14:39:11.990: W/System.err(560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
06-20 14:39:11.990: W/System.err(560):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您尝试使用f.createNewFile()创建新文件,但不创建目录。

new File(cacheImagePath).mkdirs()

答案 1 :(得分:2)

这是代码

    try {
        URL url = new URL(image_URL);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        String PATH = Environment.getExternalStorageDirectory().toString();
        Log.v("LOG_TAG", "PATH: " + PATH);
        File file = new File(PATH);
        file.mkdirs();
        String fileName = image.jpg;
        File outputFile = new File(file, fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);
        InputStream is = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();
        // Toast.makeText(this, "Downloaded Successfully", 600).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

将权限放在清单文件中

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这会将图像存储在SD卡上,然后您可以在应用程序中更快地加载它。