}else if (end.equals(“ppt”)) {
return getPptFileIntent(context,filePath);
} else if (end.equals(“xls”)) {
return getExcelFileIntent(context,filePath);
} else if (end.equals(“doc”)){
return getWordFileIntent(context,filePath);
} else if (end.equals(“txt”)) {
return getTextFileIntent(context,filePath, false);
}
else {
return getAllIntent(context,filePath);
}
}
// Android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent(Context context,String param) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri photoURI = FileProvider.getUriForFile(context,
“com.datab.cn.fileprovider”,
new File(param));//file即为所要共享的文件的file
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//授予临时权限别忘了
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(photoURI, “image/*”);
return intent;
}
// Android获取一个用于打开APK文件的intent
public static Intent getApkFileIntent(Context context,String param) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, “com.datab.cn.fileprovider”, new File(param));
intent.setDataAndType(contentUri, “application/vnd.android.package-archive”);
} else {
intent.setDataAndType(Uri.fromFile(new File(param)), “application/vnd.android.package-archive”);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
return intent;
}
// Android获取一个用于打开PPT文件的intent
public static Intent getPptFileIntent(Context context,String param) {
Intent intent = new Intent(“android.intent.action.VIEW”);
intent.addCategory(“android.intent.category.DEFAULT”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Uri uri = Uri.fromFile(new File(param));
Uri uri = FileProvider.getUriForFile(context, “com.datab.cn.fileprovider”, new File(param));
intent.setDataAndType(uri, “application/vnd.ms-powerpoint”);
return intent;
}
// Android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent(Context context,String param) {
Intent intent = new Intent(“android.intent.action.VIEW”);
intent.addCategory(“android.intent.category.DEFAULT”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Uri uri = Uri.fromFile(new File(param));
Uri uri = FileProvider.getUriForFile(context, “com.datab.cn.fileprovider”, new File(param));
intent.setDataAndType(uri, “application/vnd.ms-excel”);
return intent;
}
// Android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent(Context context,String param) {
Intent intent = new Intent(“android.intent.action.VIEW”);
intent.addCategory(“android.intent.category.DEFAULT”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Uri uri = Uri.fromFile(new File(param));
Uri uri = FileProvider.getUriForFile(context, “com.datab.cn.fileprovider”, new File(param));
intent.setDataAndType(uri, “application/msword”);
return intent;
}
public static Intent getTextFileIntent(Context context,String param, boolean paramBoolean) {
Intent intent = new Intent(“android.intent.action.VIEW”);
intent.addCategory(“android.intent.category.DEFAULT”);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (paramBoolean) {
Uri uri1 = Uri.parse(param);
intent.setDataAndType(uri1, “text/plain”);
} else {
// Uri uri2 = Uri.fromFile(new File(param));
Uri uri2 = FileProvider.getUriForFile(context, “com.datab.cn.fileprovider”, new File(param));
intent.setDataAndType(uri2, “text/plain”);
}
return intent;
}
// Android获取一个用于打开APK文件的intent
public static Intent getAllIntent(Context context,String param) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, “/”);
return intent;
}
1.ChatActivity.java
中:
private void pickImage() {
Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickIntent.setType(“image/*”);
startActivityForResult(pickIntent, REQUEST_IMAGE_PICKER);
}
如下图:
2.发送图片后把图片放在手机文件管理里
1.(/storage/emulated/0/Android/data/包名/files/Pictures/sent
),FileUtils.java
中:
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
2.ChatActivity.java
中:
private File createSentImageFile(Context context, String fileName, Bitmap bitmap) throws IOException {
file = new File(FileUtils.getSentImagesDir(context), fileName + FileUtils.IMAGE_EXTENSION);
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
return file;
}
3.FileUtils.java
中:
public static File getSentImagesDir(Context context) {
File dir = new File(getImagesDir(context), “sent”);
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
…
private static File getImagesDir(Context context) {
return isExternalStorageWritable() ? context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) : context.getDir(“images”, Context.MODE_PRIVATE);
}
如下图:
1.txt文件
2.apk文件
1.ChatActivity.java
中:
private void pickFile() {
Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickIntent.setType("/");
pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(pickIntent, REQUEST_FILE_PICKER);
}
如下图:
2.发送图片后把图片放在手机文件管理里:ChatActivity
中:
private File createSentFile(Context context, String filename, String fileend, byte[] bytes) {
String end = null;
if(fileend.equals(FileUtils.IMAGE_EXTENSION)) {
end = fileend;
}else if (fileend.equals(FileUtils.FILE_GIF_EXTENSION)) {
end = fileend;
}else if (fileend.equals(FileUtils.FILE_PNG_EXTENSION)) {
end = fileend;
}else if (fileend.equals(FileUtils.FILE_JPEG_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_APK_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_PPT_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_XLS_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_DOC_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_TXT_EXTENSION)){
end = fileend;
}
file = new File(FileUtils.getSentFileDir(context), filename + end);
try {
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(bytes);
outStream.close();
} catch (IOException e) {
e.printStackTrace();
_XLS_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_DOC_EXTENSION)){
end = fileend;
}else if (fileend.equals(FileUtils.FILE_TXT_EXTENSION)){
end = fileend;
}
file = new File(FileUtils.getSentFileDir(context), filename + end);
try {
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(bytes);
outStream.close();
} catch (IOException e) {
e.printStackTrace();