From f75adde63fe75a3a15fcb0cdafd9d84c8b3ea8a5 Mon Sep 17 00:00:00 2001 From: luo yang <2351386755@qq.com> Date: Wed, 1 Aug 2018 15:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BF=9D=E5=AD=98=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=88=B0=E7=9B=B8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruisiapp/activity/ViewImgActivity.java | 90 ++++++++++++++++++- .../yluo/ruisiapp/api/entity/ApiResult.java | 3 - .../ruisiapp/api/entity/ForumThreadlist.java | 1 - .../yluo/ruisiapp/api/entity/Variables.java | 2 - .../ruisiapp/myhttp/ApiResponseHandler.java | 1 - .../yluo/ruisiapp/myhttp/SyncHttpClient.java | 1 - .../widget/htmlview/DefaultImageGetter.java | 2 +- .../ruisiapp/widget/htmlview/spann/Quote.java | 1 - app/src/main/res/drawable/ic_save_24dp.xml | 9 ++ app/src/main/res/layout/activity_view_img.xml | 23 ++++- 10 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 app/src/main/res/drawable/ic_save_24dp.xml diff --git a/app/src/main/java/me/yluo/ruisiapp/activity/ViewImgActivity.java b/app/src/main/java/me/yluo/ruisiapp/activity/ViewImgActivity.java index a1b6d0e5..95ae649d 100644 --- a/app/src/main/java/me/yluo/ruisiapp/activity/ViewImgActivity.java +++ b/app/src/main/java/me/yluo/ruisiapp/activity/ViewImgActivity.java @@ -1,15 +1,23 @@ package me.yluo.ruisiapp.activity; +import android.Manifest; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.graphics.Color; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.provider.MediaStore; +import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityOptionsCompat; +import android.support.v4.content.ContextCompat; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -22,6 +30,7 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -68,6 +77,22 @@ protected void onCreate(Bundle savedInstanceState) { adapter = new MyAdapter(); pager.setAdapter(adapter); + findViewById(R.id.btn_save).setOnClickListener(v -> { + if (currentPosition < 0) return; + + if (ContextCompat.checkSelfPermission(ViewImgActivity.this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + ActivityCompat.requestPermissions(ViewImgActivity.this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + 100); + } else { + saveToGallery(datas.get(currentPosition)); + } + }); + + Bundle b = getIntent().getExtras(); String url = b.getString("url"); @@ -125,8 +150,11 @@ public void finish() { } } + private int currentPosition = -1; + private void changeIndex(int pos) { index.setText((pos + 1) + "/" + datas.size()); + currentPosition = pos; } @@ -155,8 +183,9 @@ public int getCount() { return datas == null ? 0 : datas.size(); } + @NonNull @Override - public Object instantiateItem(ViewGroup container, final int position) { + public Object instantiateItem(@NonNull ViewGroup container, final int position) { ScaleImageView v = container.findViewWithTag(position); if (v == null) { v = new ScaleImageView(ViewImgActivity.this); @@ -177,18 +206,71 @@ public Object instantiateItem(ViewGroup container, final int position) { @Override - public boolean isViewFromObject(View view, Object object) { + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @Override - public int getItemPosition(Object object) { + public int getItemPosition(@NonNull Object object) { return POSITION_NONE; } @Override - public void destroyItem(ViewGroup container, int position, Object object) { + public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { + } + } + + private void saveToGallery(String url) { + new AsyncTask() { + + @Override + protected Boolean doInBackground(Void... voids) { + Bitmap bitmap = null; + try { + bitmap = Picasso.get().load(url).get(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + + // Save image to gallery + String savedImageURL = MediaStore.Images.Media.insertImage( + getContentResolver(), + bitmap, + "IMAGE_" + System.currentTimeMillis(), + null + ); + + Log.d("===", "saved " + savedImageURL); + return true; + } + + @Override + protected void onPostExecute(Boolean aBoolean) { + super.onPostExecute(aBoolean); + if (aBoolean) { + showToast("保存图片成功"); + } else { + showToast("保存图片失败"); + } + + } + }.execute(); + } + + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { + switch (requestCode) { + case 100: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + saveToGallery(datas.get(currentPosition)); + } else { + showToast("你拒绝了保存到相册的权限,无法保存"); + } + } } } } diff --git a/app/src/main/java/me/yluo/ruisiapp/api/entity/ApiResult.java b/app/src/main/java/me/yluo/ruisiapp/api/entity/ApiResult.java index ad6fc601..dc1515d8 100644 --- a/app/src/main/java/me/yluo/ruisiapp/api/entity/ApiResult.java +++ b/app/src/main/java/me/yluo/ruisiapp/api/entity/ApiResult.java @@ -1,8 +1,5 @@ package me.yluo.ruisiapp.api.entity; -import java.util.Date; - - public class ApiResult { public int Version; diff --git a/app/src/main/java/me/yluo/ruisiapp/api/entity/ForumThreadlist.java b/app/src/main/java/me/yluo/ruisiapp/api/entity/ForumThreadlist.java index 7ce32eeb..8cd08996 100644 --- a/app/src/main/java/me/yluo/ruisiapp/api/entity/ForumThreadlist.java +++ b/app/src/main/java/me/yluo/ruisiapp/api/entity/ForumThreadlist.java @@ -1,6 +1,5 @@ package me.yluo.ruisiapp.api.entity; -import java.util.Date; import java.util.List; public class ForumThreadlist { diff --git a/app/src/main/java/me/yluo/ruisiapp/api/entity/Variables.java b/app/src/main/java/me/yluo/ruisiapp/api/entity/Variables.java index 71dda1c3..2e708758 100644 --- a/app/src/main/java/me/yluo/ruisiapp/api/entity/Variables.java +++ b/app/src/main/java/me/yluo/ruisiapp/api/entity/Variables.java @@ -1,7 +1,5 @@ package me.yluo.ruisiapp.api.entity; -import java.util.List; - public class Variables { public String cookiepre; diff --git a/app/src/main/java/me/yluo/ruisiapp/myhttp/ApiResponseHandler.java b/app/src/main/java/me/yluo/ruisiapp/myhttp/ApiResponseHandler.java index 45f88cca..5cde8b5c 100644 --- a/app/src/main/java/me/yluo/ruisiapp/myhttp/ApiResponseHandler.java +++ b/app/src/main/java/me/yluo/ruisiapp/myhttp/ApiResponseHandler.java @@ -7,7 +7,6 @@ import java.lang.reflect.Type; import me.yluo.ruisiapp.api.entity.ApiResult; -import me.yluo.ruisiapp.api.entity.Variables; public abstract class ApiResponseHandler extends ResponseHandler { diff --git a/app/src/main/java/me/yluo/ruisiapp/myhttp/SyncHttpClient.java b/app/src/main/java/me/yluo/ruisiapp/myhttp/SyncHttpClient.java index 95b94930..751d5b3d 100644 --- a/app/src/main/java/me/yluo/ruisiapp/myhttp/SyncHttpClient.java +++ b/app/src/main/java/me/yluo/ruisiapp/myhttp/SyncHttpClient.java @@ -10,7 +10,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; -import java.nio.ByteBuffer; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; diff --git a/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/DefaultImageGetter.java b/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/DefaultImageGetter.java index ae281f5e..b5b64c6d 100644 --- a/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/DefaultImageGetter.java +++ b/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/DefaultImageGetter.java @@ -271,7 +271,7 @@ public Drawable bmpToDrawable(String source, Bitmap b) { private Drawable getPlaceHolder(String souce) { - ColorDrawable colorDrawable = new ColorDrawable(0xffcccccc); + ColorDrawable colorDrawable = new ColorDrawable(0x00000000); if (souce == null || souce.isEmpty()) { colorDrawable.setBounds(0, 0, 120, 120); } else if (souce.startsWith(SMILEY_PREFIX)) { diff --git a/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/spann/Quote.java b/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/spann/Quote.java index 297a1478..e0d8aa0c 100644 --- a/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/spann/Quote.java +++ b/app/src/main/java/me/yluo/ruisiapp/widget/htmlview/spann/Quote.java @@ -1,6 +1,5 @@ package me.yluo.ruisiapp.widget.htmlview.spann; -import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.support.v4.content.ContextCompat; diff --git a/app/src/main/res/drawable/ic_save_24dp.xml b/app/src/main/res/drawable/ic_save_24dp.xml new file mode 100644 index 00000000..a561d632 --- /dev/null +++ b/app/src/main/res/drawable/ic_save_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_view_img.xml b/app/src/main/res/layout/activity_view_img.xml index d5ccf5e1..ea8e40e5 100644 --- a/app/src/main/res/layout/activity_view_img.xml +++ b/app/src/main/res/layout/activity_view_img.xml @@ -35,12 +35,12 @@ android:layout_marginEnd="50dp" android:layout_marginStart="50dp" android:ellipsize="end" - android:textStyle="bold" android:gravity="center" android:maxLines="1" android:text="@string/app_name" android:textColor="@color/white" - android:textSize="18sp" /> + android:textSize="18sp" + android:textStyle="bold" /> + +