Android transfers images or other files in the memory card to another path.
This is a simple problem, but it is also a problem we often encounter, that is, how to store images or other files on the memory card during Android development, then we can process this file. Because we cannot process the original file, we are currently working on an android project for images. So I have used this knowledge to share it with you.
private void string2File() {tempFiles = new File[resultFileList.size()];passFileMap = new HashMap
();for (int i = 0; i < resultFileList.size(); ++i) {String name = resultFileList.get(i).substring(resultFileList.get(i).lastIndexOf("/") + 1);name = getCacheDir(mContext) + "/" + name;File file = new File(resultFileList.get(i));tempFiles[i] = new File(name);Uri uri = Uri.fromFile(tempFiles[i]);try {Bitmap bitmap = decodeFile(file, 1000);if (!tempFiles[i].exists()) {tempFiles[i].createNewFile();}FileOutputStream out = new FileOutputStream(tempFiles[i]);bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);out.flush();out.close();passFileMap.put(tempFiles[i].getAbsolutePath(), tempFiles[i]);// tempFiles[i].delete();} catch (Exception e) {// TODO: handle exception}}}private Bitmap decodeFile(File f, int bmpsize) {if (f == null || !f.exists())return null;try {BitmapFactory.Options o = new BitmapFactory.Options();o.inJustDecodeBounds = true;o.inPreferredConfig = Bitmap.Config.ARGB_8888;o.inInputShareable = true;o.inPurgeable = true;BitmapFactory.decodeStream(new FileInputStream(f), null, o);final int REQUIRED_SIZE = bmpsize;int width_tmp = o.outWidth, height_tmp = o.outHeight;int scale = 1;if (width_tmp > REQUIRED_SIZE || height_tmp > REQUIRED_SIZE) {if (width_tmp > height_tmp) {scale = width_tmp / REQUIRED_SIZE;} else {scale = height_tmp / REQUIRED_SIZE;}}// decode with inSampleSizeBitmapFactory.Options o2 = new BitmapFactory.Options();o2.inSampleSize = scale;o2.inPreferredConfig = Bitmap.Config.ARGB_8888;o2.inInputShareable = true;o2.inPurgeable = true;Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),null, o2);return bmp;} catch (FileNotFoundException e) {}return null;}
We first extract the original file and perform specific processing on it. For example, we can compress the image and store the processed image in the new file, this will not affect the original file.
This situation is very common in processing local image compression. I hope to give you some inspiration.