package cn.flightfeather.thirdapp.util.file;
|
|
import android.content.Context;
|
import android.database.Cursor;
|
import android.media.MediaScannerConnection;
|
import android.net.Uri;
|
import android.os.Environment;
|
import android.provider.MediaStore;
|
|
import java.io.BufferedInputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
|
import cn.flightfeather.thirdapp.bean.Mediafile;
|
|
public class FileUtil {
|
|
//复制文件
|
public static void copyFile(File oldfile, File newfile) throws IOException {
|
FileInputStream ins = new FileInputStream(oldfile);
|
FileOutputStream out = new FileOutputStream(newfile);
|
//自定义缓冲对象
|
byte[] b = new byte[1024];
|
int n = 0;
|
while ((n = ins.read(b)) != -1) {
|
out.write(b, 0, b.length);
|
}
|
ins.close();
|
out.close();
|
|
System.out.println("copy success");
|
}
|
|
/**
|
* 根据路径获取本机的图片
|
* @param mediafile
|
* @return
|
*/
|
public static File getFileFromMediaFile(Mediafile mediafile){
|
File file = new File(Environment.getExternalStorageDirectory(), (mediafile.getPath() + mediafile.getDescription()));
|
return file;
|
}
|
|
//将图片写入磁盘
|
public static File writeResponseBodyToDisk(Context context, File file, Mediafile mediafile) {
|
try {
|
InputStream is = new FileInputStream(file);
|
File osFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),(mediafile.getPath() + mediafile.getDescription()));
|
if (osFile.exists()) {
|
osFile.delete();
|
osFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),(mediafile.getPath() + mediafile.getDescription()) );
|
}
|
osFile.getParentFile().mkdirs();
|
FileOutputStream fos = new FileOutputStream(osFile);
|
BufferedInputStream bis = new BufferedInputStream(is);
|
byte[] buffer = new byte[1024];
|
int len;
|
while ((len = bis.read(buffer)) != -1) {
|
fos.write(buffer, 0, len);
|
}
|
fos.flush();
|
fos.close();
|
bis.close();
|
is.close();
|
return file;
|
} catch (IOException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
//将图片插入到系统图库
|
public static String sendMediaScanBroadcast(Context context, File file) {
|
try {
|
String url = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null);
|
String[] paths = new String[]{file.getAbsolutePath()};
|
MediaScannerConnection.scanFile(context, paths, null, null);
|
// Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
// Uri uri = Uri.fromFile(file);
|
// intent.setData(uri);
|
// context.sendBroadcast(intent);
|
return url;
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static String getRealPathFromUri(Context context, Uri contentUri) {
|
Cursor cursor = null;
|
try {
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
// String id = contentUri.getLastPathSegment().split(":")[1];
|
// final String[] imageColumns = {MediaStore.Images.Media.DATA };
|
// final String imageOrderBy = null;
|
// Cursor imageCursor = context.getContentResolver().query(contentUri, imageColumns,
|
// MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
|
// if (imageCursor.moveToFirst()) {
|
// return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
|
// }else{
|
// return null;
|
// }
|
// } else {
|
String[] proj = {MediaStore.Images.Media.DATA};
|
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
|
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
cursor.moveToFirst();
|
return cursor.getString(column_index);
|
// }
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (cursor != null) {
|
cursor.close();
|
}
|
}
|
return "";
|
}
|
}
|