@Override public void onClick(View v) { switch (v.getId()) { case R.id.button_detail_share: disableSingleTap = true; File imageFile = getImageFile(uris.get(viewPager.getCurrentItem()), "分享"); if (imageFile != null) { Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile)); intent.setType("image/" + parseFileType(imageFile.getName())); List<ResolveInfo> infoList = getActivity().getPackageManager().queryIntentActivities(intent, 0); if (infoList != null && !infoList.isEmpty()) { startActivity(intent); } else { Toast.makeText(getActivity(), "您的设备上没有能够分享的APP", Toast.LENGTH_LONG).show(); } } break; case R.id.button_detail_applyWallpaper: disableSingleTap = true; File imageFile2 = getImageFile(uris.get(viewPager.getCurrentItem()), "设置壁纸"); if (imageFile2 != null) { new ApplyWallpaperAsyncTask(getActivity(), imageFile2) { @Override protected void onPostExecute(Boolean aBoolean) { if (getActivity() != null) { Toast.makeText(getActivity(), aBoolean ? "设置壁纸成功" : "设置壁纸失败", Toast.LENGTH_LONG) .show(); } } }.execute(0); } break; case R.id.button_detail_play: disableSingleTap = true; viewPagerPlayer.start(); toggleToolbarVisibleState(); break; case R.id.button_detail_save: disableSingleTap = true; String currentUrl = uris.get(viewPager.getCurrentItem()); if (currentUrl == null || "".equals(currentUrl.trim())) { Toast.makeText(getActivity(), "保存图片失败,因为当前图片的URL是空的,没法拿到图片", Toast.LENGTH_LONG).show(); } else if (currentUrl.startsWith("http://") || currentUrl.startsWith("https://")) { File imageFile3 = Sketch.with(getActivity()).getConfiguration().getDiskCache().getCacheFile(currentUrl); if (imageFile3 == null || !imageFile3.exists()) { Toast.makeText(getActivity(), "图片还没有下载好哦,再等一会儿吧!", Toast.LENGTH_LONG).show(); } else { new SaveImageAsyncTask(getActivity(), imageFile3).execute(""); } } else if (currentUrl.startsWith("/")) { Toast.makeText(getActivity(), "当前图片本就是本地的无需保存", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getActivity(), "我去,怎么会有这样的URL " + currentUrl, Toast.LENGTH_LONG).show(); } break; } }
private File getImageFile(String imageUrl, String type) { String currentUrl = uris.get(viewPager.getCurrentItem()); if (currentUrl == null || "".equals(currentUrl.trim())) { Toast.makeText(getActivity(), type + ",当前图片的URL是空的,没法拿到图片", Toast.LENGTH_LONG).show(); return null; } else if (currentUrl.startsWith("http://") || currentUrl.startsWith("https://")) { File file = Sketch.with(getActivity()).getConfiguration().getDiskCache().getCacheFile(currentUrl); if (file == null || !file.exists()) { Toast.makeText(getActivity(), "图片还没有下载好哦,再等一会儿吧!", Toast.LENGTH_LONG).show(); return null; } else { return file; } } else if (currentUrl.startsWith("/")) { return new File(currentUrl); } else { Toast.makeText(getActivity(), "我去,怎么会有这样的URL " + imageUrl, Toast.LENGTH_LONG).show(); return null; } }