import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.util.Log;
public class FileOperateTools {
/**
*
* @param context
* @param title
* @param fname
* @param path
*/
@Deprecated
public static void saveImageToPhotos(Context context,String title,String fname,String path){
final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;
final String IMAGE_MIME_TYPE = "image/png";
ContentValues values = new ContentValues(4);
values.put(Images.Media.TITLE, title);
values.put(Images.Media.DISPLAY_NAME, fname);
values.put(Images.Media.MIME_TYPE, IMAGE_MIME_TYPE);
values.put(Images.Media.DATA, path);
ContentResolver contentResolver = context.getContentResolver();
contentResolver.insert(STORAGE_URI, values);
}
/**
*
* @param context
* @param src
* @param name
* @param title
* @param dir
* @return
*/
public static boolean saveBitmapToPhotos(Context context,Bitmap src,String name,String title,File dir){
File f=new File(dir,name);
try {
if(!f.exists()) {
f.createNewFile();
}
OutputStream os=new FileOutputStream(f);
src.compress(CompressFormat.JPEG, 50, os);
//saveImageToDb("VcDownload"+id, fn, f.getAbsolutePath());
//Write info. to Database
final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;
final String IMAGE_MIME_TYPE = "image/png";
ContentValues values = new ContentValues(4);
values.put(Images.Media.TITLE, title);
values.put(Images.Media.DISPLAY_NAME, name);
values.put(Images.Media.MIME_TYPE, IMAGE_MIME_TYPE);
values.put(Images.Media.DATA, f.getAbsolutePath());
ContentResolver contentResolver = context.getContentResolver();
Uri dataUri = contentResolver.insert(STORAGE_URI, values);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static int ExtractFromApk(Context mCon,String sourceDir,String destDir, boolean replace) {
ZipFile zipFile = null ;
File src_dir =new File(mCon.getApplicationInfo().sourceDir);
Log.v("Test", "src_dir="+src_dir);
File dest_dir=new File(destDir);
if(!src_dir.exists() || !dest_dir.exists()){
return -1;
}
// if ( targetPathFile.exists() ) {
// if (!isForceMove ) {
// //根文件夹存在 且不强制移动 不移动文件
// return targetPath;
// }else{
// FileUtils.deleteQuietly( targetPathFile );
// }
// }
int f_n=0;
try {
int BUFFER=1024;
//targetPathFile.mkdir();
zipFile = new ZipFile(src_dir);
Enumeration<?> emu = zipFile.entries();
int currentByte;
int sdl=sourceDir.length();
while (emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) emu.nextElement();
String ename = entry.getName();
if (ename.startsWith(sourceDir) && !entry.isDirectory()) {
String nnn=ename.substring(sdl);
File destFile = new File(dest_dir, nnn.length()>0?("/" + nnn):nnn);
File destinationParent = destFile.getParentFile();
if( !destinationParent.exists() ){
destinationParent.mkdirs();
}
BufferedInputStream is = new BufferedInputStream( zipFile.getInputStream( entry ) , BUFFER );
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
f_n++;
}
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
return f_n;
}
}
FileOperateTools
最新推荐文章于 2019-06-26 16:50:50 发布