Android transfers images or other files in the memory card to another path.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.