Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from whmsysu/master
Browse files Browse the repository at this point in the history
Fix download file permission for PictureActivity
  • Loading branch information
werbhelius committed Dec 18, 2018
2 parents 9444205 + 1779e83 commit 47181b9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 67 deletions.
56 changes: 26 additions & 30 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
# Built application files
**/build/
# built application files
*.apk
*.ap_

# Crashlytics configuations
com_crashlytics_export_strings.xml
# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties

# Gradle generated files
.gradle/
# Eclipse project files
.classpath
.project

# Signing files
.signing/

# User-specific configurations
.idea/libraries/
.idea/workspace.xml
.idea/tasks.xml
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/vcs.xml
# Android Studio
.idea/
.gradle
/*/local.properties
/*/out
/*/*/build
/*/*/production
*.iml

# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
*.iws
*.ipr
*~
*.swp

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

dependencies {
Expand All @@ -41,4 +42,5 @@ dependencies {
implementation 'com.jakewharton:butterknife:7.0.1'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.github.chrisbanes.photoview:library:1.2.3'
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.werb.gankwithzhihu.ui.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.view.ViewCompat;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.werb.gankwithzhihu.R;
import com.werb.gankwithzhihu.ui.base.BasePresenter;
import com.werb.gankwithzhihu.ui.base.MVPBaseActivity;
Expand Down Expand Up @@ -40,7 +43,9 @@ public class PictureActivity extends MVPBaseActivity {

@Bind(R.id.iv_meizhi_pic)
ImageView iv_meizhi_pic;
@OnClick(R.id.save_img) void saveImg(){

@OnClick(R.id.save_img)
void saveImg() {
saveImage();
}

Expand Down Expand Up @@ -75,42 +80,55 @@ protected void onDestroy() {
Glide.clear(iv_meizhi_pic);
}

public static Intent newIntent(Context context, String url,String desc){
Intent intent = new Intent(context,PictureActivity.class);
intent.putExtra(PictureActivity.IMG_URL,url);
intent.putExtra(PictureActivity.IMG_DESC,desc);
public static Intent newIntent(Context context, String url, String desc) {
Intent intent = new Intent(context, PictureActivity.class);
intent.putExtra(PictureActivity.IMG_URL, url);
intent.putExtra(PictureActivity.IMG_DESC, desc);
return intent;
}

private void parseIntent(){
private void parseIntent() {
img_url = getIntent().getStringExtra(IMG_URL);
img_desc = getIntent().getStringExtra(IMG_DESC);
}

private void saveImage(){
iv_meizhi_pic.buildDrawingCache();
Bitmap bitmap = iv_meizhi_pic.getDrawingCache();
//将Bitmap 转换成二进制,写入本地
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG , 100 , stream);
byte[] byteArray = stream.toByteArray();
File dir=new File(Environment.getExternalStorageDirectory ().getAbsolutePath()+"/zhigan" );
if(!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, img_desc.substring(0, 10) + ".png");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(byteArray, 0, byteArray.length);
fos.flush();
//用广播通知相册进行更新相册
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
PictureActivity.this.sendBroadcast(intent);
Toast.makeText(PictureActivity.this,"保存成功~",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
private void saveImage() {
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
if (granted) { // Always true pre-M
// I can control the camera now
iv_meizhi_pic.buildDrawingCache();
Bitmap bitmap = iv_meizhi_pic.getDrawingCache();
//将Bitmap 转换成二进制,写入本地
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/zhigan");
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, img_desc.substring(0, 10) + ".png");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(byteArray, 0, byteArray.length);
fos.flush();
//用广播通知相册进行更新相册
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
PictureActivity.this.sendBroadcast(intent);
Toast.makeText(PictureActivity.this, "保存成功~", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Oups permission denied
Toast.makeText(PictureActivity.this, "保存失败~", Toast.LENGTH_LONG).show();
}
});

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.werb.gankwithzhihu.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -66,23 +67,23 @@ public int getItemViewType(int position) {
}

@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof TopStoriesViewHolder) {
TopStoriesViewHolder topStoriesViewHolder = (TopStoriesViewHolder) holder;
topStoriesViewHolder.vp_top_stories.startAutoRun();
}
}

@Override
public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof TopStoriesViewHolder) {
TopStoriesViewHolder topStoriesViewHolder = (TopStoriesViewHolder) holder;
topStoriesViewHolder.vp_top_stories.stopAutoRun();
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_TOP) {
View rootView = View.inflate(parent.getContext(), R.layout.item_zhihu_top_stories, null);
return new TopStoriesViewHolder(rootView);
Expand All @@ -96,7 +97,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof FooterViewHolder) {
FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
footerViewHolder.bindItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class TopStoriesViewPager extends RelativeLayout {
private Context context;
private ViewPager viewPager;
private ViewPagerClickListener listenner;
private List<View> dotList;
private int currentItem = 0;// ImageViewpager当前页面的index
private List<ImageView> images;
// 执行周期性或定时任务
Expand Down Expand Up @@ -86,7 +85,6 @@ public void init(List<TopStories> items,TextView tv,
ViewPagerClickListener clickListenner) {
this.listenner = clickListenner;
images = new ArrayList<>();
dotList = new ArrayList<>();

for (int i = 0; i < items.size(); i++) {
final TopStories item = items.get(i);
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ allprojects {
repositories {
jcenter()
google()
maven { url 'https://jitpack.io' }
}
}

Expand Down

0 comments on commit 47181b9

Please sign in to comment.