forked from hzuapps/android-labs-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6107927
commit 8e5fc70
Showing
12 changed files
with
344 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 26 additions & 1 deletion
27
.../app/src/main/java/edu/hzuapps/androidlabs/com1613090502214/Com1613090502214Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...90502214/project/app/src/main/java/edu/hzuapps/androidlabs/com1613090502214/ImgUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package edu.hzuapps.androidlabs.com1613090502214 | ||
; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import android.os.Environment; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
|
||
public class ImgUtils { | ||
//保存文件到指定路径 | ||
public static boolean saveImageToGallery(Context context, Bitmap bmp) { | ||
// 首先保存图片 | ||
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo"; | ||
File appDir = new File(storePath); | ||
if (!appDir.exists()) { | ||
appDir.mkdir(); | ||
} | ||
String fileName = System.currentTimeMillis() + ".jpg"; | ||
File file = new File(appDir, fileName); | ||
try { | ||
FileOutputStream fos = new FileOutputStream(file); | ||
//通过io流的方式来压缩保存图片 | ||
boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos); | ||
fos.flush(); | ||
fos.close(); | ||
|
||
//把文件插入到系统图库 | ||
//MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); | ||
|
||
//保存图片后发送广播通知更新数据库 | ||
Uri uri = Uri.fromFile(file); | ||
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); | ||
if (isSuccess) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...214/project/app/src/main/java/edu/hzuapps/androidlabs/com1613090502214/ThirdActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package edu.hzuapps.androidlabs.com1613090502214; | ||
|
||
import android.Manifest; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ImageView; | ||
import android.widget.Toast; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
import pub.devrel.easypermissions.AppSettingsDialog; | ||
import pub.devrel.easypermissions.EasyPermissions; | ||
|
||
|
||
public class ThirdActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { | ||
|
||
private static final int REQUEST_CODE_SAVE_IMG = 10; | ||
private static final String TAG = "MainActivity"; | ||
private Context mContext; | ||
private EditText editText; | ||
private Button button; | ||
private Button button1; | ||
private ImageView imageView; | ||
private Bitmap bitmap; | ||
//手柄更新的作用 | ||
Handler handler=new Handler(){ | ||
public void handleMessage(Message msg) { | ||
if(msg.what==111){ | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
}; | ||
}; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_third); | ||
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||
//初始化组件 | ||
editText=(EditText) findViewById(R.id.editText2); | ||
button=(Button) findViewById(R.id.download); | ||
button1=(Button)findViewById(R.id.save) ; | ||
imageView=(ImageView) findViewById(R.id.imageView); | ||
|
||
mContext = this; | ||
button.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
new Thread(t).start(); | ||
|
||
} | ||
}); | ||
button1.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
requestPermission(); | ||
} | ||
}); | ||
} | ||
|
||
//为了下载图片资源,开辟一个新的子线程 | ||
Thread t=new Thread() { | ||
public void run() { | ||
//下载图片的路径 | ||
String iPath = editText.getText().toString(); | ||
try { | ||
//对资源链接 | ||
URL url = new URL(iPath); | ||
//打开输入流 | ||
InputStream inputStream = url.openStream(); | ||
//对网上资源进行下载转换位图图片 | ||
bitmap = BitmapFactory.decodeStream(inputStream); | ||
handler.sendEmptyMessage(111); | ||
inputStream.close(); | ||
}catch (MalformedURLException e){ | ||
e.printStackTrace(); | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
}; | ||
/** | ||
* 请求读取sd卡的权限 | ||
*/ | ||
private void requestPermission() { | ||
if (Build.VERSION.SDK_INT >= 23) { | ||
//读取sd卡的权限 | ||
String[] mPermissionList = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; | ||
if (EasyPermissions.hasPermissions(mContext, mPermissionList)) { | ||
//已经同意过 | ||
saveImage(bitmap); | ||
} else { | ||
//未同意过,或者说是拒绝了,再次申请权限 | ||
EasyPermissions.requestPermissions( | ||
this, //上下文 | ||
"保存图片需要读取sd卡的权限", //提示文言 | ||
REQUEST_CODE_SAVE_IMG, //请求码 | ||
mPermissionList //权限列表 | ||
); | ||
} | ||
} else { | ||
saveImage(bitmap); | ||
} | ||
} | ||
|
||
|
||
//保存图片 | ||
private void saveImage(Bitmap bitmap) { | ||
|
||
boolean isSaveSuccess = ImgUtils.saveImageToGallery(mContext, bitmap); | ||
if (isSaveSuccess) { | ||
Toast.makeText(mContext, "保存图片成功", Toast.LENGTH_SHORT).show(); | ||
} else { | ||
Toast.makeText(mContext, "保存图片失败,请稍后重试", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
//授权结果,分发下去 | ||
@Override | ||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
// Forward results to EasyPermissions | ||
//跳转到onPermissionsGranted或者onPermissionsDenied去回调授权结果 | ||
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); | ||
} | ||
|
||
|
||
//同意授权 | ||
@Override | ||
public void onPermissionsGranted(int requestCode, List<String> list) { | ||
Log.i(TAG, "onPermissionsGranted:" + requestCode + ":" + list.size()); | ||
saveImage(bitmap); | ||
} | ||
|
||
//拒绝授权 | ||
@Override | ||
public void onPermissionsDenied(int requestCode, List<String> perms) { | ||
Log.i(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); | ||
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { | ||
//打开系统设置,手动授权 | ||
new AppSettingsDialog.Builder(this).build().show(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) { | ||
//拒绝授权后,从系统设置了授权后,返回APP进行相应的操作 | ||
Log.i(TAG, "onPermissionsDenied:------>自定义设置授权后返回APP"); | ||
saveImage(bitmap); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.