package com.tejia.lijin.app.ui.trends;
|
|
import android.Manifest;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.Bundle;
|
import android.os.Environment;
|
import android.os.Handler;
|
import android.os.Message;
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import androidx.viewpager.widget.ViewPager;
|
import android.view.KeyEvent;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.wpc.library.util.common.FileUtils;
|
import com.wpc.library.util.security.MD5Utils;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.callBack.PermissionInterface;
|
import com.tejia.lijin.app.entity.common.ImageShowEntity;
|
import com.tejia.lijin.app.ui.BaseActivity;
|
import com.tejia.lijin.app.ui.ImageShowAdapter;
|
import com.tejia.lijin.app.util.PermissionHelper;
|
import com.tejia.lijin.app.util.downutil.StringUtils;
|
import com.tejia.lijin.app.util.view.PhotoViewPager;
|
import com.ysh.wpc.appupdate.download.DownLoadFile;
|
|
import java.io.File;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2018/8/22.
|
*/
|
|
public class BigImageActivity extends BaseActivity implements ImageShowAdapter.OnFinishCallBack, PermissionInterface, ImageShowAdapter.ISaveImage {
|
|
private int mCurPageIndex = 1;
|
private int mPageCount = 0;
|
|
private PhotoViewPager mViewPager;
|
private LinearLayout mPageDotLl;
|
private LinearLayout fl_img;
|
private TextView tv_page_num, tv_save;
|
List<ImageShowEntity> photoUrlList;
|
|
@Override
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setShowRecommend(false);
|
setContentView(R.layout.activity_image_big);
|
findWidget();
|
initData();
|
}
|
|
protected void findWidget() {
|
fl_img = findViewById(R.id.ll_img);
|
mViewPager = findViewById(R.id.show_origin_pic_vp);
|
mPageDotLl = findViewById(R.id.show_origin_pic_dot);
|
tv_page_num = findViewById(R.id.tv_page_num);
|
tv_save = findViewById(R.id.tv_save);
|
// fl_img.postDelayed(new Runnable() {
|
// @Override
|
// public void run() {
|
fl_img.setBackgroundColor(getResources().getColor(R.color.black));
|
mViewPager.setBackgroundColor(getResources().getColor(R.color.black));
|
// }
|
// }, 220);//400
|
}
|
|
protected boolean isFile;
|
|
private void initData() {
|
String json = getIntent().getStringExtra("imgList");
|
photoUrlList = new Gson().fromJson(json, new TypeToken<List<ImageShowEntity>>() {
|
}.getType());
|
mCurPageIndex = getIntent().getIntExtra("position", 0);
|
if (mCurPageIndex == 0) {
|
String position = getIntent().getStringExtra("position");
|
if (!StringUtils.isNullOrEmpty(position)) {
|
try {
|
mCurPageIndex = Integer.parseInt(position);
|
} catch (Exception e) {
|
|
}
|
}
|
}
|
|
mPageCount = photoUrlList.size();
|
|
ImageShowAdapter imageAdapter = new ImageShowAdapter(this, photoUrlList, this);
|
imageAdapter.setOnFinishCallBack(this);
|
|
if (photoUrlList.size() > 1) {
|
tv_page_num.setVisibility(View.VISIBLE);
|
tv_page_num.setText((mCurPageIndex + 1) + "/" + photoUrlList.size());
|
} else {
|
tv_page_num.setVisibility(View.GONE);
|
}
|
mViewPager.setAdapter(imageAdapter);
|
mViewPager.setCurrentItem(mCurPageIndex);
|
if (photoUrlList.size() > 1)
|
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
@Override
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
}
|
|
@Override
|
public void onPageSelected(int position) {
|
tv_page_num.setText((position + 1) + "/" + photoUrlList.size());
|
}
|
|
@Override
|
public void onPageScrollStateChanged(int state) {
|
|
}
|
});
|
tv_save.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
saveImage(photoUrlList.get(mCurPageIndex));
|
}
|
});
|
setOvalLayout();
|
mPermissionsChecker = new PermissionHelper(this, this);//权限
|
}
|
|
protected PermissionHelper mPermissionsChecker; // 权限检测器 检测是否有储存卡写入读取权限
|
|
/**
|
* 权限请求码
|
*
|
* @return
|
*/
|
@Override
|
public int getPermissionsRequestCode() {
|
return 1001;
|
}
|
|
/**
|
* 请求权限
|
*
|
* @return
|
*/
|
@Override
|
public String[] getPermissions() {
|
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
}
|
|
/**
|
* 权限请求成功
|
*/
|
@Override
|
public void requestPermissionsSuccess() {
|
tv_save.performClick();
|
}
|
|
/**
|
* 权限请求失败
|
*/
|
@Override
|
public void requestPermissionsFail() {
|
// Toast.makeText(this, "你以拒绝权限", Toast.LENGTH_SHORT).show();
|
}
|
|
/**
|
* 请求权限结果
|
*
|
* @param requestCode
|
* @param permissions
|
* @param grantResults
|
*/
|
@Override
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
if (mPermissionsChecker.requestPermissionsResult(requestCode, permissions, grantResults)) {
|
//权限请求结果,并已经处理了该回调
|
return;
|
}
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
}
|
|
/**
|
* 设置圆点
|
*/
|
public void setOvalLayout() {
|
if (mPageCount <= 0) {
|
return;
|
}
|
|
mPageDotLl.removeAllViews();
|
for (int i = 0; i < mPageCount; i++) {
|
mPageDotLl.addView(LayoutInflater.from(this).inflate(R.layout.vp_dot, mPageDotLl, false));
|
}
|
if (mPageCount > 1)
|
mPageDotLl.getChildAt(mCurPageIndex).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.shape_circle_white);
|
|
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
@Override
|
public void onPageSelected(int position) {
|
// 取消圆点选中
|
mPageDotLl.getChildAt(mCurPageIndex).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.shape_circle_gray);
|
// 圆点选中
|
mPageDotLl.getChildAt(position).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.shape_circle_white);
|
mCurPageIndex = position;
|
}
|
|
@Override
|
public void onPageScrolled(int arg0, float arg1, int arg2) {
|
}
|
|
@Override
|
public void onPageScrollStateChanged(int arg0) {
|
}
|
});
|
}
|
|
Handler handler = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
if (msg.what == 0) {
|
// SingleToast.showToast(BigImageActivity.this, "图片保存成功");
|
Toast.makeText(BigImageActivity.this, "图片保存成功", Toast.LENGTH_SHORT).show();
|
} else {
|
// SingleToast.showToast(BigImageActivity.this, "图片保存失败");
|
Toast.makeText(BigImageActivity.this, "图片保存失败", Toast.LENGTH_SHORT).show();
|
}
|
}
|
};
|
|
@Override
|
public void finishShowPic() {
|
setResult(2);
|
mViewPager.setBackgroundColor(getResources().getColor(R.color.transport));
|
finish();
|
// overridePendingTransition(0, R.anim.activity_zoom_close);
|
}
|
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
finishShowPic();
|
}
|
return super.onKeyDown(keyCode, event);
|
}
|
|
@Override
|
public void saveImage(final ImageShowEntity entity) {
|
if (mPermissionsChecker.lacksPermissions(getPermissions())) { //检测权限
|
mPermissionsChecker.showNormalDialog();
|
return;
|
}
|
|
if (!StringUtils.isNullOrEmpty(entity.getFilePath())) {//保存本地图片
|
try {
|
String fileName = FileUtils.MD5(entity.getFilePath());
|
String path = Environment.getExternalStorageDirectory().getPath() + "/BanLiImg/" + fileName + ".png";
|
FileUtils.copyFile(new File(entity.getFilePath()), new File(path));
|
handler.sendEmptyMessage(0);
|
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
Uri uri = Uri.fromFile(new File(path));
|
intent.setData(uri);
|
sendBroadcast(intent);
|
} catch (Exception e) {
|
handler.sendEmptyMessage(1);
|
}
|
} else {//保存网络图片
|
final String fileName = MD5Utils.getMD532(entity.getUrl());
|
// String path = Environment.getExternalStorageDirectory()
|
// + File.separator + Environment.DIRECTORY_DCIM
|
// + File.separator + "Camera" + File.separator + fileName + ".png";
|
|
final String path = Environment.getExternalStorageDirectory().getPath() + "/BanLiImg/" + fileName + ".png";
|
final File file = new File(path);
|
if (!file.isDirectory() && file.exists()) {
|
Toast.makeText(this, "文件已保存", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
|
new Thread() {
|
@Override
|
public void run() {
|
DownLoadFile df = new DownLoadFile();
|
File image = new File(file.getParentFile().getAbsolutePath(), file.getName());
|
try {
|
df.downLoadFile(null, image.getPath(), entity.getUrl(), BigImageActivity.this);
|
handler.sendEmptyMessage(0);
|
} catch (Exception e) {
|
e.printStackTrace();
|
handler.sendEmptyMessage(1);
|
}
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
Uri uri = Uri.fromFile(image);
|
intent.setData(uri);
|
sendBroadcast(intent);
|
}
|
}.start();
|
}
|
|
}
|
}
|