110 lines
3.9 KiB
Java
110 lines
3.9 KiB
Java
package com.uiuipad.os.uiuiutils;
|
|
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.net.Uri;
|
|
import android.provider.MediaStore;
|
|
import android.text.TextUtils;
|
|
|
|
import java.io.File;
|
|
import java.util.HashSet;
|
|
|
|
public class FileUtil {
|
|
public static String getFileType(String url) {
|
|
if (url.indexOf("/") == -1) {
|
|
return url.substring(url.indexOf("."), url.length());
|
|
} else {
|
|
String fileName = url.substring(url.lastIndexOf("/"));
|
|
return fileName.substring(fileName.indexOf("."), fileName.length());
|
|
}
|
|
}
|
|
|
|
public static boolean isLocalPath(String path) {
|
|
return path.startsWith(File.separator);
|
|
}
|
|
|
|
private static HashSet<String> videoFormat = new HashSet<String>() {{
|
|
this.add(".mp4");
|
|
this.add(".avi");
|
|
this.add(".nkv");
|
|
this.add(".flv");
|
|
}};
|
|
private static HashSet<String> pictureFormat = new HashSet<String>() {{
|
|
this.add(".png");
|
|
this.add(".jpg");
|
|
this.add(".jpeg");
|
|
this.add(".bmp");
|
|
}};
|
|
|
|
public static boolean isVideoFile(String fileName) {
|
|
if (TextUtils.isEmpty(fileName)) {
|
|
return false;
|
|
} else {
|
|
if (!fileName.startsWith(".")) {
|
|
return videoFormat.contains(getFileType(fileName));
|
|
} else {
|
|
return videoFormat.contains(fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isPictureFile(String fileName) {
|
|
if (TextUtils.isEmpty(fileName)) {
|
|
return false;
|
|
} else {
|
|
if (!fileName.startsWith(".")) {
|
|
return pictureFormat.contains(getFileType(fileName));
|
|
} else {
|
|
return pictureFormat.contains(fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static File uriToFile(Uri uri, Context context) {
|
|
String path = null;
|
|
if ("file".equals(uri.getScheme())) {
|
|
path = uri.getEncodedPath();
|
|
if (path != null) {
|
|
path = Uri.decode(path);
|
|
ContentResolver cr = context.getContentResolver();
|
|
StringBuffer buff = new StringBuffer();
|
|
buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
|
|
Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA}, buff.toString(), null, null);
|
|
int index = 0;
|
|
int dataIdx = 0;
|
|
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
|
|
index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
|
|
index = cur.getInt(index);
|
|
dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
|
|
path = cur.getString(dataIdx);
|
|
}
|
|
cur.close();
|
|
if (index == 0) {
|
|
} else {
|
|
Uri u = Uri.parse("content://media/external/images/media/" + index);
|
|
System.out.println("temp uri is :" + u);
|
|
}
|
|
}
|
|
if (path != null) {
|
|
return new File(path);
|
|
}
|
|
} else if ("content".equals(uri.getScheme())) {
|
|
// 4.2.2以后
|
|
String[] proj = {MediaStore.Images.Media.DATA};
|
|
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
|
|
if (cursor.moveToFirst()) {
|
|
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
path = cursor.getString(columnIndex);
|
|
}
|
|
cursor.close();
|
|
|
|
return new File(path);
|
|
} else {
|
|
//Log.i(TAG, "Uri Scheme:" + uri.getScheme());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|