Android開發(fā)中的文件操作工具類FileUtil完整實例
本文實例講述了Android開發(fā)中的文件操作工具類FileUtil。分享給大家供大家參考,具體如下:
package com.ymerp.android.tools;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
/**
* 文件操作工具
*
* @author chen.lin
*
*/
public class FileUtil {
private static final String TAG = "FileUtil";
/**
* 從sd卡取文件
*
* @param filename
* @return
*/
public String getFileFromSdcard(String filename) {
ByteArrayOutputStream outputStream = null;
FileInputStream fis = null;
try {
outputStream = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory(), filename);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
fis = new FileInputStream(file);
int len = 0;
byte[] data = new byte[1024];
while ((len = fis.read(data)) != -1) {
outputStream.write(data, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
fis.close();
} catch (IOException e) {
}
}
return new String(outputStream.toByteArray());
}
/**
* 保存文件到sd
*
* @param filename
* @param content
* @return
*/
public static boolean saveContentToSdcard(String filename, String content) {
boolean flag = false;
FileOutputStream fos = null;
try {
File file = new File(Environment.getExternalStorageDirectory(), filename);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
fos = new FileOutputStream(file);
fos.write(content.getBytes());
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
flag = false;
} finally {
try {
fos.close();
} catch (IOException e) {
}
}
return flag;
}
/**
* 取得文件大小
*
* @param f
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public static long getFileSizes(File f) throws Exception {
long size = 0;
if (f.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(f);
size = fis.available();
} else {
f.createNewFile();
}
return size;
}
/**
* 遞歸取得文件夾大小
*
* @param dir
* @return
* @throws Exception
*/
public static long getFileSize(File dir) throws Exception {
long size = 0;
File flist[] = dir.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSize(flist[i]);
} else {
size = size + flist[i].length();
}
}
return size;
}
/**
* 轉(zhuǎn)換文件大小
*
* @param fileS
* @return
*/
public static String FormetFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
/**
* 遞歸求取目錄文件個數(shù)
*
* @param f
* @return
*/
public static long getlist(File f) {
long size = 0;
File flist[] = f.listFiles();
size = flist.length;
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getlist(flist[i]);
size--;
}
}
return size;
}
/**
* 在根目錄下搜索文件
*
* @param keyword
* @return
*/
public static String searchFile(String keyword) {
String result = "";
File[] files = new File("/").listFiles();
for (File file : files) {
if (file.getName().indexOf(keyword) >= 0) {
result += file.getPath() + "\n";
}
}
if (result.equals("")) {
result = "找不到文件!!";
}
return result;
}
/**
* @detail 搜索sdcard文件
* @param 需要進行文件搜索的目錄
* @param 過濾搜索文件類型
* */
public static List<String> search(File file, String[] ext) {
List<String> list = new ArrayList<String>();
if (file != null) {
if (file.isDirectory()) {
File[] listFile = file.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
search(listFile[i], ext);
}
}
} else {
String filename = file.getAbsolutePath();
for (int i = 0; i < ext.length; i++) {
if (filename.endsWith(ext[i])) {
list.add(filename);
break;
}
}
}
}
return list;
}
/**
* 查詢文件
*
* @param file
* @param keyword
* @return
*/
public static List<File> FindFile(File file, String keyword) {
List<File> list = new ArrayList<File>();
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File tempf : files) {
if (tempf.isDirectory()) {
if (tempf.getName().toLowerCase().lastIndexOf(keyword) > -1) {
list.add(tempf);
}
list.addAll(FindFile(tempf, keyword));
} else {
if (tempf.getName().toLowerCase().lastIndexOf(keyword) > -1) {
list.add(tempf);
}
}
}
}
}
return list;
}
/**
* searchFile 查找文件并加入到ArrayList 當中去
*
* @param context
* @param keyword
* @param filepath
* @return
*/
public static List<Map<String, Object>> searchFile(Context context, String keyword, File filepath) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> rowItem = null;
int index = 0;
// 判斷SD卡是否存在
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File[] files = filepath.listFiles();
if (files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
if (file.getName().toLowerCase().lastIndexOf(keyword) > -1) {
rowItem = new HashMap<String, Object>();
rowItem.put("number", index); // 加入序列號
rowItem.put("fileName", file.getName());// 加入名稱
rowItem.put("path", file.getPath()); // 加入路徑
rowItem.put("size", file.length() + ""); // 加入文件大小
list.add(rowItem);
}
// 如果目錄可讀就執(zhí)行(一定要加,不然會掛掉)
if (file.canRead()) {
list.addAll(searchFile(context, keyword, file)); // 如果是目錄,遞歸查找
}
} else {
// 判斷是文件,則進行文件名判斷
try {
if (file.getName().indexOf(keyword) > -1 || file.getName().indexOf(keyword.toUpperCase()) > -1) {
rowItem = new HashMap<String, Object>();
rowItem.put("number", index); // 加入序列號
rowItem.put("fileName", file.getName());// 加入名稱
rowItem.put("path", file.getPath()); // 加入路徑
rowItem.put("size", file.length() + ""); // 加入文件大小
list.add(rowItem);
index++;
}
} catch (Exception e) {
Toast.makeText(context, "查找發(fā)生錯誤!", Toast.LENGTH_SHORT).show();
}
}
}
}
}
return list;
}
/**
* 根據(jù)后綴得到文件類型
*
* @param fileName
* @param pointIndex
* @return
*/
public static String getFileType(String fileName, int pointIndex) {
String type = fileName.substring(pointIndex + 1).toLowerCase();
if ("m4a".equalsIgnoreCase(type) || "xmf".equalsIgnoreCase(type) || "ogg".equalsIgnoreCase(type) || "wav".equalsIgnoreCase(type)
|| "m4a".equalsIgnoreCase(type) || "aiff".equalsIgnoreCase(type) || "midi".equalsIgnoreCase(type)
|| "vqf".equalsIgnoreCase(type) || "aac".equalsIgnoreCase(type) || "flac".equalsIgnoreCase(type)
|| "tak".equalsIgnoreCase(type) || "wv".equalsIgnoreCase(type)) {
type = "ic_file_audio";
} else if ("mp3".equalsIgnoreCase(type) || "mid".equalsIgnoreCase(type)) {
type = "ic_file_mp3";
} else if ("avi".equalsIgnoreCase(type) || "mp4".equalsIgnoreCase(type) || "dvd".equalsIgnoreCase(type)
|| "mid".equalsIgnoreCase(type) || "mov".equalsIgnoreCase(type) || "mkv".equalsIgnoreCase(type)
|| "mp2v".equalsIgnoreCase(type) || "mpe".equalsIgnoreCase(type) || "mpeg".equalsIgnoreCase(type)
|| "mpg".equalsIgnoreCase(type) || "asx".equalsIgnoreCase(type) || "asf".equalsIgnoreCase(type)
|| "flv".equalsIgnoreCase(type) || "navi".equalsIgnoreCase(type) || "divx".equalsIgnoreCase(type)
|| "rm".equalsIgnoreCase(type) || "rmvb".equalsIgnoreCase(type) || "dat".equalsIgnoreCase(type)
|| "mpa".equalsIgnoreCase(type) || "vob".equalsIgnoreCase(type) || "3gp".equalsIgnoreCase(type)
|| "swf".equalsIgnoreCase(type) || "wmv".equalsIgnoreCase(type)) {
type = "ic_file_video";
} else if ("bmp".equalsIgnoreCase(type) || "pcx".equalsIgnoreCase(type) || "tiff".equalsIgnoreCase(type)
|| "gif".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type) || "tga".equalsIgnoreCase(type)
|| "exif".equalsIgnoreCase(type) || "fpx".equalsIgnoreCase(type) || "psd".equalsIgnoreCase(type)
|| "cdr".equalsIgnoreCase(type) || "raw".equalsIgnoreCase(type) || "eps".equalsIgnoreCase(type)
|| "gif".equalsIgnoreCase(type) || "jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)
|| "png".equalsIgnoreCase(type) || "hdri".equalsIgnoreCase(type) || "ai".equalsIgnoreCase(type)) {
type = "ic_file_image";
} else if ("ppt".equalsIgnoreCase(type) || "doc".equalsIgnoreCase(type) || "xls".equalsIgnoreCase(type)
|| "pps".equalsIgnoreCase(type) || "xlsx".equalsIgnoreCase(type) || "xlsm".equalsIgnoreCase(type)
|| "pptx".equalsIgnoreCase(type) || "pptm".equalsIgnoreCase(type) || "ppsx".equalsIgnoreCase(type)
|| "maw".equalsIgnoreCase(type) || "mdb".equalsIgnoreCase(type) || "pot".equalsIgnoreCase(type)
|| "msg".equalsIgnoreCase(type) || "oft".equalsIgnoreCase(type) || "xlw".equalsIgnoreCase(type)
|| "wps".equalsIgnoreCase(type) || "rtf".equalsIgnoreCase(type) || "ppsm".equalsIgnoreCase(type)
|| "potx".equalsIgnoreCase(type) || "potm".equalsIgnoreCase(type) || "ppam".equalsIgnoreCase(type)) {
type = "ic_file_office";
} else if ("txt".equalsIgnoreCase(type) || "text".equalsIgnoreCase(type) || "chm".equalsIgnoreCase(type)
|| "hlp".equalsIgnoreCase(type) || "pdf".equalsIgnoreCase(type) || "doc".equalsIgnoreCase(type)
|| "docx".equalsIgnoreCase(type) || "docm".equalsIgnoreCase(type) || "dotx".equalsIgnoreCase(type)) {
type = "ic_file_text";
} else if ("ini".equalsIgnoreCase(type) || "sys".equalsIgnoreCase(type) || "dll".equalsIgnoreCase(type)
|| "adt".equalsIgnoreCase(type)) {
type = "ic_file_system";
} else if ("rar".equalsIgnoreCase(type) || "zip".equalsIgnoreCase(type) || "arj".equalsIgnoreCase(type)
|| "gz".equalsIgnoreCase(type) || "z".equalsIgnoreCase(type) || "7Z".equalsIgnoreCase(type) || "GZ".equalsIgnoreCase(type)
|| "BZ".equalsIgnoreCase(type) || "ZPAQ".equalsIgnoreCase(type)) {
type = "ic_file_rar";
} else if ("html".equalsIgnoreCase(type) || "htm".equalsIgnoreCase(type) || "java".equalsIgnoreCase(type)
|| "php".equalsIgnoreCase(type) || "asp".equalsIgnoreCase(type) || "aspx".equalsIgnoreCase(type)
|| "jsp".equalsIgnoreCase(type) || "shtml".equalsIgnoreCase(type) || "xml".equalsIgnoreCase(type)) {
type = "ic_file_web";
} else if ("exe".equalsIgnoreCase(type) || "com".equalsIgnoreCase(type) || "bat".equalsIgnoreCase(type)
|| "iso".equalsIgnoreCase(type) || "msi".equalsIgnoreCase(type)) {
type = "ic_file_exe";
} else if ("apk".equalsIgnoreCase(type)) {
type = "ic_file_apk";
} else {
type = "ic_file_normal";
}
return type;
}
/**
* 改變文件大小顯示的內(nèi)容
*
* @param size
* @return
*/
public static String changeFileSize(String size) {
if (Integer.parseInt(size) > 1024) {
size = Integer.parseInt(size) / 1024 + "K";
} else if (Integer.parseInt(size) > (1024 * 1024)) {
size = Integer.parseInt(size) / (1024 * 1024) + "M";
} else if (Integer.parseInt(size) > (1024 * 1024 * 1024)) {
size = Integer.parseInt(size) / (1024 * 1024 * 1024) + "G";
} else {
size += "B";
}
return size;
}
/**
* 得到所有文件
*
* @param dir
* @return
*/
public static ArrayList<File> getAllFiles(File dir) {
ArrayList<File> allFiles = new ArrayList<File>();
// 遞歸取得目錄下的所有文件及文件夾
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
allFiles.add(file);
if (file.isDirectory()) {
getAllFiles(file);
}
}
Logger.i("test", allFiles.size() + "");
return allFiles;
}
/**
* 判斷文件MimeType 類型
*
* @param f
* @return
*/
public static String getMIMEType(File f) {
String type = "";
String fName = f.getName();
/* 取得擴展名 */
String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();
/* 依擴展名的類型決定MimeType */
if (end.equalsIgnoreCase("m4a") || end.equalsIgnoreCase("mp3") || end.equalsIgnoreCase("mid") || end.equalsIgnoreCase("xmf")
|| end.equalsIgnoreCase("ogg") || end.equalsIgnoreCase("wav")) {
type = "audio";
} else if (end.equalsIgnoreCase("3gp") || end.equalsIgnoreCase("mp4")) {
type = "video";
} else if (end.equalsIgnoreCase("jpg") || end.equalsIgnoreCase("gif") || end.equalsIgnoreCase("png")
|| end.equalsIgnoreCase("jpeg") || end.equalsIgnoreCase("bmp")) {
type = "image";
} else if (end.equalsIgnoreCase("apk")) {
/* android.permission.INSTALL_PACKAGES */
type = "application/vnd.android.package-archive";
} else if (end.equalsIgnoreCase("txt") || end.equalsIgnoreCase("java")) {
/* android.permission.INSTALL_PACKAGES */
type = "text";
} else {
type = "*";
}
/* 如果無法直接打開,就跳出軟件列表給用戶選擇 */
if (end.equalsIgnoreCase("apk")) {
} else {
type += "/*";
}
return type;
}
/**
* 拷貝文件
*
* @param fromFile
* @param toFile
* @throws IOException
*/
public static void copyFile(File fromFile, String toFile) throws IOException {
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
} finally {
if (from != null)
try {
from.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
if (to != null)
try {
to.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
}
/**
* 創(chuàng)建文件
*
* @param file
* @return
*/
public static File createNewFile(File file) {
try {
if (file.exists()) {
return file;
}
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
Log.e(TAG, "", e);
return null;
}
return file;
}
/**
* 創(chuàng)建文件
*
* @param path
*/
public static File createNewFile(String path) {
File file = new File(path);
return createNewFile(file);
}// end method createText()
/**
* 刪除文件
*
* @param path
*/
public static void deleteFile(String path) {
File file = new File(path);
deleteFile(file);
}
/**
* 刪除文件
*
* @param file
*/
public static void deleteFile(File file) {
if (!file.exists()) {
return;
}
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFile(files[i]);
}
}
file.delete();
}
/**
* 向Text文件中寫入內(nèi)容
*
* @param file
* @param content
* @return
*/
public static boolean write(String path, String content) {
return write(path, content, false);
}
public static boolean write(String path, String content, boolean append) {
return write(new File(path), content, append);
}
public static boolean write(File file, String content) {
return write(file, content, false);
}
/**
* 寫入文件
*
* @param file
* @param content
* @param append
* @return
*/
public static boolean write(File file, String content, boolean append) {
if (file == null || StringUtil.empty(content)) {
return false;
}
if (!file.exists()) {
file = createNewFile(file);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, append);
fos.write(content.getBytes());
} catch (Exception e) {
Log.e(TAG, "", e);
return false;
} finally {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
fos = null;
}
return true;
}
/**
* 獲得文件名
*
* @param path
* @return
*/
public static String getFileName(String path) {
if (StringUtil.empty(path)) {
return null;
}
File f = new File(path);
String name = f.getName();
f = null;
return name;
}
/**
* 讀取文件內(nèi)容,從第startLine行開始,讀取lineCount行
*
* @param file
* @param startLine
* @param lineCount
* @return 讀到文字的list,如果list.size<lineCount則說明讀到文件末尾了
*/
public static List<String> readFile(File file, int startLine, int lineCount) {
if (file == null || startLine < 1 || lineCount < 1) {
return null;
}
if (!file.exists()) {
return null;
}
FileReader fileReader = null;
List<String> list = null;
try {
list = new ArrayList<String>();
fileReader = new FileReader(file);
LineNumberReader lineReader = new LineNumberReader(fileReader);
boolean end = false;
for (int i = 1; i < startLine; i++) {
if (lineReader.readLine() == null) {
end = true;
break;
}
}
if (end == false) {
for (int i = startLine; i < startLine + lineCount; i++) {
String line = lineReader.readLine();
if (line == null) {
break;
}
list.add(line);
}
}
} catch (Exception e) {
Log.e(TAG, "read log error!", e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
/**
* 創(chuàng)建文件夾
*
* @param dir
* @return
*/
public static boolean createDir(File dir) {
try {
if (!dir.exists()) {
dir.mkdirs();
}
return true;
} catch (Exception e) {
Log.e(TAG, "create dir error", e);
return false;
}
}
/**
* 在SD卡上創(chuàng)建目錄
*
* @param dirName
*/
public static File creatSDDir(String dirName) {
File dir = new File(dirName);
dir.mkdir();
return dir;
}
/**
* 判斷SD卡上的文件是否存在
*/
public static boolean isFileExist(String fileName) {
File file = new File(fileName);
return file.exists();
}
/**
* 將一個InputStream里面的數(shù)據(jù)寫入到SD卡中
*/
public static File write2SDFromInput(String path, String fileName, InputStream input) {
File file = null;
OutputStream output = null;
try {
creatSDDir(path);
file = createNewFile(path + "/" + fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[1024];
int len = -1;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
/**
* 讀取文件內(nèi)容 從文件中一行一行的讀取文件
*
* @param file
* @return
*/
public static String readFile(File file) {
Reader read = null;
String content = "";
String result = "";
BufferedReader br = null;
try {
read = new FileReader(file);
br = new BufferedReader(read);
while ((content = br.readLine().toString().trim()) != null) {
result += content + "\r\n";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
read.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 將圖片保存到本地時進行壓縮, 即將圖片從Bitmap形式變?yōu)镕ile形式時進行壓縮,
* 特點是: File形式的圖片確實被壓縮了, 但是當你重新讀取壓縮后的file為 Bitmap是,它占用的內(nèi)存并沒有改變
*
* @param bmp
* @param file
*/
public static void compressBmpToFile(Bitmap bmp, File file) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 80;// 個人喜歡從80開始,
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
options -= 10;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 將圖片從本地讀到內(nèi)存時,進行壓縮 ,即圖片從File形式變?yōu)锽itmap形式
* 特點: 通過設(shè)置采樣率, 減少圖片的像素, 達到對內(nèi)存中的Bitmap進行壓縮
* @param srcPath
* @return
*/
public static Bitmap compressImageFromFile(String srcPath, float pixWidth, float pixHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 只讀邊,不讀內(nèi)容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, options);
options.inJustDecodeBounds = false;
int w = options.outWidth;
int h = options.outHeight;
//float pixWidth = 800f;//
//float pixHeight = 480f;//
int scale = 1;
if (w > h && w > pixWidth) {
scale = (int) (options.outWidth / pixWidth);
} else if (w < h && h > pixHeight) {
scale = (int) (options.outHeight / pixHeight);
}
if (scale <= 0)
scale = 1;
options.inSampleSize = scale;// 設(shè)置采樣率
options.inPreferredConfig = Config.ARGB_8888;// 該模式是默認的,可不設(shè)
options.inPurgeable = true;// 同時設(shè)置才會有效
options.inInputShareable = true;// 。當系統(tǒng)內(nèi)存不夠時候圖片自動被回收
bitmap = BitmapFactory.decodeFile(srcPath, options);
// return compressBmpFromBmp(bitmap);//原來的方法調(diào)用了這個方法企圖進行二次壓縮
// 其實是無效的,大家盡管嘗試
return bitmap;
}
/**
* 指定分辨率和清晰度的圖片壓縮
*/
public void transImage(String fromFile, String toFile, int width, int height, int quality)
{
try
{
Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
// 縮放圖片的尺寸
float scaleWidth = (float) width / bitmapWidth;
float scaleHeight = (float) height / bitmapHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 產(chǎn)生縮放后的Bitmap對象
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
// save file
File myCaptureFile = new File(toFile);
FileOutputStream out = new FileOutputStream(myCaptureFile);
if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)){
out.flush();
out.close();
}
if(!bitmap.isRecycled()){
bitmap.recycle();//記得釋放資源,否則會內(nèi)存溢出
}
if(!resizeBitmap.isRecycled()){
resizeBitmap.recycle();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Win10下android studio開發(fā)環(huán)境配置圖文教程
這篇文章主要為大家詳細介紹了Win10下android studio開發(fā)環(huán)境配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Material Design系列之Behavior實現(xiàn)支付密碼彈窗和商品屬性選擇效果
這篇文章主要為大家詳細介紹了Material Design系列之Behavior實現(xiàn)支付密碼彈窗和商品屬性選擇效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android使用ShareSDK實現(xiàn)應(yīng)用分享的功能
這篇文章主要為大家詳細介紹了Android使用ShareSDK實現(xiàn)應(yīng)用分享的功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android 6.0以上權(quán)限拒絕打開權(quán)限設(shè)置界面的解決方法
今天小編就為大家分享一篇Android 6.0以上權(quán)限拒絕打開權(quán)限設(shè)置界面的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
FragmentTabHost FrameLayout實現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細介紹了FragmentTabHost和FrameLayout實現(xiàn)底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
讓Android應(yīng)用不被殺死(killer)的方法
這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實現(xiàn)方法和原理分析,需要的朋友可以參考下2015-04-04

