package cn.flightfeather.thirdapp.activity; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bm.library.PhotoView; import com.ping.greendao.gen.MediafileDao; import java.io.File; import java.util.List; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.adapter.CommonPagerAdapter; import cn.flightfeather.thirdapp.bean.Mediafile; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.http.DELETE; import retrofit2.http.Path; public class PhotoViewerActivity extends AppCompatActivity implements View.OnClickListener{ private PhotoView pv_main; private TextView tv_name; private ImageView iv_delete; private ImageView iv_share; private TextView tv_currentP; private TextView tv_totalCount; private ViewPager view_pager; private int type; private boolean deletable; private File file; private List files; private List mediafiles; private Mediafile mediafile; private int position; public static final int CAMERA_PHOTO = 10; public static final int CAMERA_PHOTO_TEMP =14; public static final int SIGN_PHOTO =11; public static final int EVIDENCE_PHOTO = 12; public static final int EVIDENCE_PHOTO_TEMP =16; public static final int GIT_PHOTO =15; public static final int GIT_PHOTO_TEMP=13; public static final int PROBLEM_PHOTO =17; public static final int CHANGE_PHOTO =18; public static final String PARA_FILES = "files"; public static final String PARA_MEDIAS = "mediaFiles"; private CommonApplication application; private PhotoViewerService photoViewerService; private MediafileDao mediafileDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo_viewer); application = (CommonApplication) getApplication(); initUI(); initData2(); } //初始化界面 private void initUI() { tv_currentP = findViewById(R.id.tv_currentP); tv_totalCount = findViewById(R.id.tv_totalCount); tv_name = (TextView) findViewById(R.id.tv_name); iv_delete = (ImageView) findViewById(R.id.iv_delete); iv_share = (ImageView) findViewById(R.id.iv_share); view_pager = findViewById(R.id.view_pager); } //初始化数据 // private void initData() { // photoViewerService = (PhotoViewerService) application.getRetrofit().create(PhotoViewerService.class); // mediafileDao = application.getDaoSession().getMediafileDao(); // // Intent intent = getIntent(); // file = (File) intent.getSerializableExtra("file"); // type = intent.getIntExtra("type",0); // deletable = intent.getBooleanExtra("deletable",false); // // // // if (file!=null){ // Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath()); // pv_main.setImageBitmap(bm); // // String name = file.getName(); // if (name.length()>8){ // tv_name.setTitle(name.substring(0,name.length()-8)); // }else { // tv_name.setTitle(name); // } // // // iv_share.setOnClickListener(this); // if (!deletable){ // iv_delete.setVisibility(View.GONE); // }else { // mediafile = (Mediafile) intent.getSerializableExtra("mediaFile"); // if (mediafile ==null&&type!= GIT_PHOTO_TEMP&&type!=CAMERA_PHOTO_TEMP&&type!=EVIDENCE_PHOTO_TEMP){ // Toast.makeText(application, "mediaFile获取失败", Toast.LENGTH_SHORT).show(); // iv_delete.setVisibility(View.GONE); // } // position = intent.getIntExtra("position",-1); // iv_delete.setOnClickListener(this); // } // // } // } private void initData2() { photoViewerService = (PhotoViewerService) application.getRetrofit().create(PhotoViewerService.class); mediafileDao = application.getDaoSession().getMediafileDao(); Intent intent = getIntent(); files = (List) intent.getSerializableExtra(PARA_FILES); type = intent.getIntExtra("type",0); deletable = intent.getBooleanExtra("deletable",false); mediafiles = (List) intent.getSerializableExtra(PARA_MEDIAS); position = intent.getIntExtra("position",-1); iv_share.setOnClickListener(this); if (!deletable){ iv_delete.setVisibility(View.GONE); }else { if (mediafiles ==null&&type!= GIT_PHOTO_TEMP&&type!=CAMERA_PHOTO_TEMP&&type!=EVIDENCE_PHOTO_TEMP){ Toast.makeText(application, "mediaFile获取失败", Toast.LENGTH_SHORT).show(); iv_delete.setVisibility(View.GONE); } iv_delete.setOnClickListener(this); } if (files != null && files.size() > 0) { tv_currentP.setText(String.valueOf(position + 1)); tv_totalCount.setText(String.valueOf(files.size())); String name = files.get(position).getName(); if (name.length() > 8) { tv_name.setText(name.substring(0, name.length() - 8)); } else { tv_name.setText(name); } view_pager.setAdapter(new CommonPagerAdapter(files, this)); view_pager.setCurrentItem(position); view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { String name = files.get(i).getName(); if (name.length() > 8) { tv_name.setText(name.substring(0, name.length() - 8)); } else { tv_name.setText(name); } position = i; tv_currentP.setText(String.valueOf(i + 1)); } @Override public void onPageScrollStateChanged(int i) { } }); } } //分享单张图片 private void shareSingPhoto(File file){ //分享图片 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); } Uri imageUri = Uri.fromFile(file); // Uri imageUri = FileProvider.getUriForFile(getApplication(), getApplicationContext().getPackageName() + ".fileProvider", file); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享一张图片")); } //显示删除的dialog private void showDeleteDialog(){ AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("要删除这张照片吗?"); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { delete(type); } }); dialog.setNegativeButton("取消",null); dialog.show(); } private void delete(final int type){ //postion有值则删除代码写在跳转前actiivty中,否则立即删除 if (type ==GIT_PHOTO_TEMP||type ==CAMERA_PHOTO_TEMP||type == EVIDENCE_PHOTO_TEMP){ if (position>-1){ Intent intent = new Intent(); intent.putExtra("position",position); setResult(RESULT_OK,intent); finish(); } }else { final Mediafile m = mediafiles.get(position); if (m.getRemark().equals("未上传")){ if (type ==CAMERA_PHOTO||type ==SIGN_PHOTO||type ==GIT_PHOTO||type ==PROBLEM_PHOTO||type ==CHANGE_PHOTO){ mediafileDao.deleteByKey(m.getGuid()); files.get(position).delete(); Toast.makeText(application, "删除成功(local)", Toast.LENGTH_SHORT).show(); setDeleteResult(type); finish(); } }else if (m.getRemark().equals("已上传")){ Call deletePhoto = photoViewerService.deleteMediaFile(m.getGuid()); deletePhoto.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body()!=null){ if (type == CAMERA_PHOTO||type == SIGN_PHOTO||type == GIT_PHOTO||type ==PROBLEM_PHOTO||type ==CHANGE_PHOTO){ mediafileDao.deleteByKey(m.getGuid()); Toast.makeText(application, "删除成功", Toast.LENGTH_SHORT).show(); files.get(position).delete(); setDeleteResult(type); finish(); } }else if (response.errorBody()!=null){ Toast.makeText(application, "删除失败", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call call, Throwable t) { Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show(); } }); } } } //将要删除的请求返回给上层来删除 private void setDeleteResult(int type){ Intent intent = new Intent(); intent.putExtra("position",position); setResult(RESULT_OK,intent); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.iv_delete: showDeleteDialog(); break; case R.id.iv_share: shareSingPhoto(files.get(position)); } } interface PhotoViewerService{ @DELETE("mediafile/{MediaFileGUID}/ ") Call deleteMediaFile(@Path("MediaFileGUID") String mediaFileGuid); } }