Skip to content

Commit

Permalink
hzuapps#6 #1370第六次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
PickForSth committed May 22, 2018
1 parent 6107927 commit 8e5fc70
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 6 deletions.
Binary file added com1613090502214/#61.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added com1613090502214/#62.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added com1613090502214/#63.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added com1613090502214/#64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions com1613090502214/project/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
<activity android:name=".Com1613090502214Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity"/>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="andorid.permission.MONUN_UNMOUNT_FILESYSTEMS"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
package edu.hzuapps.androidlabs.com1613090502214;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class Com1613090502214Activity extends AppCompatActivity {
private TextView textView;
private MenuItem setbk;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1613090502214);

textView=(TextView)findViewById(R.id.button);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent intent=new Intent(Com1613090502214Activity.this,SecondActivity.class);
startActivity(intent);
}});
setbk=(MenuItem) findViewById(R.id.action_setbk);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.setbk, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_setbk:
Intent intent=new Intent();
intent.setClass(Com1613090502214Activity.this,ThirdActivity.class);
startActivity(intent);
Com1613090502214Activity.this.finish();
break;
}
return super.onOptionsItemSelected(item);
}

}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
Expand All @@ -21,6 +22,7 @@ public class SecondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
textView=(TextView)findViewById(R.id.button3);
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Expand All @@ -37,8 +39,17 @@ public void onClick(View view) {
save(text1);
}
});
}

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
public void save(String text){
FileOutputStream out=null;
BufferedWriter writer=null;
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></ListView>
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"></ListView>

<Button
android:id="@+id/button"
Expand All @@ -28,4 +30,6 @@
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />


</android.support.constraint.ConstraintLayout>
Loading

0 comments on commit 8e5fc70

Please sign in to comment.