-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2606 from ZhuHongen/master
- Loading branch information
Showing
7 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
soft1614080902146/app/src/main/java/com/example/hzu/myapplication/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 com.example.administrator.myapplication; | ||
|
||
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; | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
soft1614080902146/app/src/main/java/com/example/hzu/myapplication/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 com.example.administrator.myapplication; | ||
|
||
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); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
soft1614080902146/app/src/main/res/layout/activity_third.xml
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,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".ThirdActivity"> | ||
|
||
|
||
<ImageView | ||
android:id="@+id/imageView" | ||
android:layout_width="352dp" | ||
android:layout_height="347dp" | ||
android:layout_marginBottom="28dp" | ||
android:layout_marginEnd="3dp" | ||
android:layout_marginLeft="2dp" | ||
android:layout_marginRight="3dp" | ||
android:layout_marginStart="2dp" | ||
android:layout_marginTop="50dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.507" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/download" /> | ||
|
||
<Button | ||
android:id="@+id/download" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="86dp" | ||
android:layout_marginStart="86dp" | ||
android:layout_marginTop="16dp" | ||
android:text="下载" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/editText2" /> | ||
|
||
<EditText | ||
android:id="@+id/editText2" | ||
android:layout_width="363dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="11dp" | ||
android:layout_marginLeft="10dp" | ||
android:layout_marginRight="11dp" | ||
android:layout_marginStart="10dp" | ||
android:layout_marginTop="16dp" | ||
android:ems="10" | ||
android:hint="输入网址" | ||
android:inputType="textPersonName" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_chainStyle="spread" /> | ||
|
||
<Button | ||
android:id="@+id/save" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="84dp" | ||
android:layout_marginRight="84dp" | ||
android:text="保存" | ||
app:layout_constraintBaseline_toBaselineOf="@+id/download" | ||
app:layout_constraintEnd_toEndOf="parent" /> | ||
|
||
|
||
</android.support.constraint.ConstraintLayout> |
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,37 @@ | ||
### 实验六 | ||
|
||
## 一.实验目的 | ||
|
||
掌握Android网络访问方法; | ||
|
||
## 二.实验内容 | ||
|
||
从网络下载一个文件(图片、MP3、MP4); | ||
保存到手机,在应用中使用文件; | ||
将应用运行结果截图。 | ||
|
||
## 三.实验步骤 | ||
|
||
1.先修改AndroidManifest.xml获取相关的权限 | ||
2.写一个工具类imgUtils,调用它可以保存图片到本地相册. | ||
3.开辟线程下载图片,将图片转换成位图送给imageView,调用requestPermission获取权限,通过后调用saveImage保存图片到本地相册. | ||
4.使用下载按钮获取URL的图片,点击保存保存图片到本地 | ||
5.使用Git将代码提交到自己的库中:https://github.com/PickForSth/android-labs-2018 | ||
|
||
$ git pull origin master<br> | ||
$ git add soft16130905022146<br> | ||
$ git commit "#5 #1370第6次实验"<br> | ||
$ git push<br> | ||
6.在GitHub中使用Markdown文件编写实验报告(report6.md). | ||
7.在自己的GitHub库上创建和发送Pull Request(注意查看Changed files). | ||
|
||
## 四.实验截图 | ||
|
||
![image](https://github.com/ZhuHongen/android-labs-2018/blob/master/soft1614080902146/report61.JPG) | ||
|
||
![image](https://github.com/ZhuHongen/android-labs-2018/blob/master/soft1614080902146/report62.JPG) | ||
|
||
![image](https://github.com/ZhuHongen/android-labs-2018/blob/master/soft1614080902146/report63.JPG) | ||
## 五.实验体会 | ||
|
||
通过这次实验使我更加熟悉git和android studio,为App加一个menu,利用toolbar实现Activity间的切换,开辟线程获取URL进而获取网上的资源文件 |
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.