public static void viewFile(final Context context, final String filePath, FileViewInteractionHub fileViewInteractionHub) {
if (ArchiveHelper.checkIfArchive(filePath)) {
viewArchive(context, filePath, fileViewInteractionHub);
return;
}
String type = getMimeType(filePath);
String fileRealPath = filePath;
if (!TextUtils.isEmpty(type) && !TextUtils.equals(type, "*/*")) {
/* 设置intent的file与MimeType */
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileRealPath)), type);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e("IntentBuilder", "fail to view file, type: " + type);
Toast.makeText(context, R.string.msg_unable_open_file, Toast.LENGTH_SHORT).show();
}
} else {
// unknown MimeType
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle(R.string.dialog_select_type);
CharSequence[] menuItemArray = new CharSequence[] {
context.getString(R.string.dialog_type_text),
context.getString(R.string.dialog_type_audio),
context.getString(R.string.dialog_type_video),
context.getString(R.string.dialog_type_image) };
dialogBuilder.setItems(menuItemArray,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String selectType = "*/*";
switch (which) {
case 0:
selectType = "text/*";
break;
case 1:
selectType = "audio/*";
break;
case 2:
selectType = "video/*";
break;
case 3:
selectType = "image/*";
break;
}
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filePath)), selectType);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e("IntentBuilder", "fail to view file, type: " + intent.getType());
Toast.makeText(context, R.string.msg_unable_open_file, Toast.LENGTH_SHORT).show();
}
}
});
dialogBuilder.show();
}
}
private static void viewArchive(Context context, final String filePath, final FileViewInteractionHub fileViewInteractionHub){
if(fileViewInteractionHub == null || fileViewInteractionHub.mTabIndex == 0){
return;
}
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
dialogBuilder.setTitle(fileName);
CharSequence[] menuItemArray = new CharSequence[] {
context.getString(R.string.decompress_to_current_dir),
context.getString(R.string.decompress_to)};
dialogBuilder.setItems(menuItemArray,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
fileViewInteractionHub.mDeComPressFilePath = filePath;
switch (which) {
case 0:
fileViewInteractionHub.onOperationDeCompress();
break;
case 1:
fileViewInteractionHub.updateBarForDeCompress();
break;
}
}
});
dialogBuilder.show();
}
public static Intent buildSendFile(ArrayList<FileInfo> files) {
ArrayList<Uri> uris = new ArrayList<Uri>();
String mimeType = null;
String preMineType = null;
boolean isSameMineType = true;
int sameStringleng = 0;
for (FileInfo file : files) {
if (file.IsDir)
continue;
File fileIn = new File(file.filePath);
//modified by tyd mingjun for support bluetooth to send apk
// mimeType = getSendMimeType(file.fileName);
mimeType = "audio/mpeg";
if(preMineType == null){
sameStringleng = mimeType.indexOf("/");
preMineType = mimeType.substring(0,sameStringleng + 1);
}else {
if(mimeType.startsWith(preMineType) && isSameMineType){
// preMineType = mimeType;
}else {
isSameMineType = false;
}
}
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
if (uris.size() == 0)
return null;
boolean multiple = uris.size() > 1;
Intent intent = new Intent(multiple ? android.content.Intent.ACTION_SEND_MULTIPLE
: android.content.Intent.ACTION_SEND);
if (multiple) {
if(isSameMineType){
intent.setType(mimeType);
}else {
intent.setType("*/*");
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
} else {
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
}
try {
startActivity(Intent.createChooser(intent, mContext.getString(R.string.send_file)));
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "fail to view file: " + e.toString());
}
return intent;
}
private static String getMimeType(String filePath) {
int dotPosition = filePath.lastIndexOf('.');
if (dotPosition == -1)
return "*/*";
String ext = filePath.substring(dotPosition + 1, filePath.length()).toLowerCase();
String mimeType = MimeUtils.guessMimeTypeFromExtension(ext);
return mimeType != null ? mimeType : "*/*";
}
private static String getSendMimeType(String filePath) {
int dotPosition = filePath.lastIndexOf('.');
if (dotPosition == -1)
return "application/*";
String ext = filePath.substring(dotPosition + 1, filePath.length()).toLowerCase();
String mimeType = MimeUtils.guessMimeTypeFromExtension(ext);
return mimeType != null ? mimeType : "application/*";
}
}