Skip to content

Commit

Permalink
新增保存图片到相册
Browse files Browse the repository at this point in the history
  • Loading branch information
freedom10086 committed Aug 1, 2018
1 parent 7a4f482 commit f75adde
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 18 deletions.
90 changes: 86 additions & 4 deletions app/src/main/java/me/yluo/ruisiapp/activity/ViewImgActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -125,8 +150,11 @@ public void finish() {
}
}

private int currentPosition = -1;

private void changeIndex(int pos) {
index.setText((pos + 1) + "/" + datas.size());
currentPosition = pos;
}


Expand Down Expand Up @@ -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);
Expand All @@ -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<Void, Void, Boolean>() {

@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("你拒绝了保存到相册的权限,无法保存");
}
}
}
}
}
3 changes: 0 additions & 3 deletions app/src/main/java/me/yluo/ruisiapp/api/entity/ApiResult.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package me.yluo.ruisiapp.api.entity;

import java.util.Date;


public class ApiResult<T> {

public int Version;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.yluo.ruisiapp.api.entity;

import java.util.Date;
import java.util.List;

public class ForumThreadlist {
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/me/yluo/ruisiapp/api/entity/Variables.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.yluo.ruisiapp.api.entity;

import java.util.List;

public class Variables {

public String cookiepre;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends ResponseHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_save_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM12,19c-1.66,0 -3,-1.34 -3,-3s1.34,-3 3,-3 3,1.34 3,3 -1.34,3 -3,3zM15,9L5,9L5,5h10v4z"/>
</vector>
23 changes: 19 additions & 4 deletions app/src/main/res/layout/activity_view_img.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<ImageView
android:id="@+id/menu"
Expand All @@ -58,10 +58,25 @@
android:id="@+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="12dp"
android:textColor="@color/white"
android:textColor="@color/text_color_sec"
android:textSize="20sp"
tools:text="5/6" />

<ImageView
android:id="@+id/btn_save"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="8dp"
android:layout_margin="12dp"
android:tint="@color/text_color_sec"
android:src="@drawable/ic_save_24dp" />

</FrameLayout>

0 comments on commit f75adde

Please sign in to comment.