环信聊天时长按图片保存到本地
程序员文章站
2024-02-10 15:52:16
...
在EaseShowBigImageActivity中,显示大图片,对大图片进行长按监听
1.在ease_photo_view_save_pic.xml布局文件中添加TextView控件,点击监听,保存图片
<TextView
android:visibility="invisible"
android:id="@+id/ease_photo_view_save_pic"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:background="#DDDDDD"
android:gravity="center"
android:text="保存图片" />
2.找到控件
TextView textView = (TextView) findViewById(R.id.ease_photo_view_save_pic);
EasePhotoView image = (EasePhotoView) findViewById(R.id.image);
3.处理监听
image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
textView.setVisibility(View.VISIBLE);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
textView.setVisibility(View.INVISIBLE);
new Thread(new Runnable() {
@Override
public void run() {
MyFileUtil fileUtil = new MyFileUtil (EaseShowBigImageActivity.this);
try {
//保存到本地
final File saveFile = fileUtil .save2local(bitmap);
if (saveFile != null) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(saveFile)));//更新相册
Toast.makeText(EaseShowBigImageActivity.this, "下载成功" + saveFile.toString(), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}){}.start();
}
});
return true;
}
});
4.保存文件
public class MyFileUtil {
/**
* sd卡的根目录
*/
private static String mSdRootPath = Environment.getExternalStorageDirectory().getPath();
/**
* 手机的缓存根目录
*/
private static String mDataRootPath = null;
/**
* 保存Image的目录名
*/
private final static String FOLDER_NAME = "/temp";
public MyFileUtil (Context context) {
mDataRootPath = context.getCacheDir().getPath();
}
/**
* 获取储存Image的目录
*
* @return
*/
public static String getStorageDirectory() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? mSdRootPath + FOLDER_NAME : mDataRootPath + FOLDER_NAME;
}
/**
* 根据文件名获得或者创建一个文件
*
* @param fileName
* @return
* @throws IOException
*/
public static File getFirstFile(String fileName) throws IOException {
String path = getStorageDirectory();
File folderFile = new File(path);
if (!folderFile.exists()) {
folderFile.mkdirs();
}
File file = new File(path + File.separator + fileName);
file.createNewFile();
return file;
}
/**
* 创建一个文件夹
*/
public void makeDirs() {
String path = getStorageDirectory();
File folderFile = new File(path);
if (!folderFile.exists()) {
folderFile.mkdirs();
}
}
/**
* 保存Image的方法,有sd卡存储到sd卡,没有就存储到手机目录
*
* @param bitmap
* @throws IOException
*/
public static File save2local(Bitmap bitmap) throws IOException {
if (bitmap == null) {
return null;
}
String currentTime = String.valueOf(System.currentTimeMillis());
File file = getFirstFile(currentTime);
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.flush();
fos.close();
return file;
}
}