Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.net.Uri; import java.io.File; import java.security.MessageDigest; public class Main { private static final char[] HEX_ARRAY = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** * Get a reference to a copied file * @param context Application Context * @param imageUri Source URI of an image * @return Reference to a copied file */ public static File getCopiedFile(Context context, String imageUri) { String filename; try { filename = String.format("%s.png", md5(imageUri)); } catch (Exception e) { filename = Uri.parse(imageUri).getLastPathSegment(); } File workingDir = getWorkingDir(context); File f = null; if (workingDir != null && workingDir.exists()) { f = new File(workingDir, filename); } return f; } private static String md5(String message) throws Exception { MessageDigest md = MessageDigest.getInstance("md5"); return bytesToHex(md.digest(message.getBytes("utf-8"))); } /** * Get APNG working directory * @param context Application Context * @return Reference to the working directory */ public static File getWorkingDir(Context context) { File workingDir = null; File cacheDir = context.getExternalCacheDir(); if (cacheDir == null) { cacheDir = context.getCacheDir(); } if (cacheDir != null) { workingDir = new File(String.format("%s/apng/.nomedia/", cacheDir.getPath())); if (!workingDir.exists()) { workingDir.mkdirs(); } } return workingDir; } private static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; int v; for (int j = 0; j < bytes.length; j++) { v = bytes[j] & 0xFF; hexChars[j * 2] = HEX_ARRAY[v >>> 4]; hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; } return new String(hexChars); } }