您的位置:首页 > 移动开发 > Android开发

【Android】图片/文件的处理方法

2016-10-26 14:56 363 查看
public static Bitmap rotateBitmap(Bitmap bmp, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);  //旋转
matrix.postScale(-1, 1);  //镜像水平翻转
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

private void copyBigDataToSD(String strOutFileName) throws IOException
{
File fData = new File(strOutFileName);
try{
if (!fData.exists()){   //not exist, copy
InputStream myInput;
OutputStream myOutput = new FileOutputStream(strOutFileName);
myInput = this.getAssets().open("data.bin");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}

myOutput.flush();
myInput.close();
myOutput.close();
}
}catch (Exception e ){
Log.e(TAG, "file data.bin ERROR!");
}
}

private void copyBigDataToSD1(String strOutFileName) throws IOException
{
File fData = new File(strOutFileName);
try{
if (!fData.exists()){   //not exist, copy
InputStream myInput;
OutputStream myOutput = new FileOutputStream(strOutFileName);
myInput = this.getAssets().open("data1.bin");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}

myOutput.flush();
myInput.close();
myOutput.close();
}
}catch (Exception e ){
Log.e(TAG, "file data1.bin ERROR!");
}
}

//创建文件夹
private void CreateFolder(String foldername) throws IOException{
try{
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File sd = Environment.getExternalStorageDirectory();
String path = sd.getPath() + "/App/" + foldername;
File file = new File(path);
if (!file.exists()){
file.mkdir();
}
}
}catch (Exception e ){
Log.e(TAG, "create folder ERROR!");
}
}

public void saveBitmap(Bitmap bm, String dirPath, String picName) {
File f = new File( dirPath , picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//length用户要求产生字符串的长度
public static String getRandomString(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}

public static Bitmap getScaledBitmap(Bitmap m_img, int sx, int sy) {
Matrix matrix = new Matrix();
int imgW = m_img.getWidth();
int imgH = m_img.getHeight();

float fx = (float)imgW / sx;
float fy = (float)imgH / sy;
matrix.postScale(fx, fy);
Bitmap rst = Bitmap.createBitmap(m_img, 0, 0, m_img.getWidth(), m_img.getHeight(), matrix, true);
return rst;
}

// 获取当前目录下所有的mp4文件
public static Vector<String> getJPGFileName(String fileAbsolutePath) {
Vector<String> vecFile = new Vector<String>();
File file = new File(fileAbsolutePath);
File[] subFile = file.listFiles();

for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
// 判断是否为文件夹
if (!subFile[iFileLength].isDirectory()) {
String filename = subFile[iFileLength].getName();
// 判断是否为JPG结尾
if (filename.trim().toLowerCase().endsWith(".jpg")) {
vecFile.add(filename);
}
}
}
return vecFile;
}

private Bitmap getDiskBitmap(String pathString) {
Bitmap bitmap = null;
try {
File file = new File(pathString);
if (file.exists()) {
bitmap = BitmapFactory.decodeFile(pathString);
}
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}


// 获取当前目录下所有的jpg文件
public static Vector<String> getJPGFileName(String fileAbsolutePath, Boolean isFolder) {
Vector<String> vecFile = new Vector<String>();
File file = new File(fileAbsolutePath);
File[] subFile = file.listFiles();

for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
if ( !isFolder ){
// 判断是否为文件夹
if (!subFile[iFileLength].isDirectory()) {
String filename = subFile[iFileLength].getName();
// 判断是否为JPG结尾
if (filename.trim().toLowerCase().endsWith(".jpg")) {
vecFile.add(filename);
}
}
}else{
if (subFile[iFileLength].isDirectory()) {
String filename = subFile[iFileLength].getName();
vecFile.add(filename);
}
}

}
return vecFile;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: