-
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 #2619 from Im-a-programmer/master
- Loading branch information
Showing
9 changed files
with
208 additions
and
5 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.
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 |
---|---|---|
@@ -1,21 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.com1614080901140"> | ||
package="edu.hzuapps.androidlabs.com1614080901140"> | ||
|
||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name="edu.hzuapps.andriodlabs.com1614080901140.Com1614080901140Activity"> | ||
<activity android:name=".Com1614080901140Activity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity android:name="edu.hzuapps.andriodlabs.com1614080901140.SecondActivity"></activity> | ||
<activity | ||
android:name=".SecondActivity" | ||
android:label="@string/title_activity_second" | ||
android:theme="@style/AppTheme.NoActionBar" /> | ||
<activity | ||
android:name=".ThirdActivity" | ||
android:label="@string/title_activity_third" | ||
android:theme="@style/AppTheme.NoActionBar"></activity> | ||
</application> | ||
|
||
</manifest> |
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
115 changes: 115 additions & 0 deletions
115
...14080901140/app/src/main/java/edu/hzuapps/androidlabs/Com1614080901140/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,115 @@ | ||
package edu.hzuapps.androidlabs.com1614080901140; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import android.os.AsyncTask; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.app.Activity; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.Toast; | ||
|
||
|
||
public class ThirdActivity extends AppCompatActivity implements OnClickListener{ | ||
|
||
Button content; | ||
ImageView image; | ||
Bitmap bitmap; | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_third); | ||
content=(Button)findViewById(R.id.content); | ||
image=(ImageView)findViewById(R.id.image); | ||
|
||
content.setOnClickListener(this);; | ||
image.setOnClickListener(this);; | ||
} | ||
|
||
public Bitmap GetImageInputStream(String imageurl){ | ||
URL url; | ||
HttpURLConnection connection=null; | ||
Bitmap bitmap=null; | ||
try { | ||
url = new URL(imageurl); | ||
connection=(HttpURLConnection)url.openConnection(); | ||
connection.setConnectTimeout(6000); | ||
connection.setDoInput(true); | ||
connection.setUseCaches(false); | ||
InputStream inputStream=connection.getInputStream(); | ||
bitmap=BitmapFactory.decodeStream(inputStream); | ||
inputStream.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return bitmap; | ||
} | ||
|
||
public void onClick(View v) { | ||
switch (v.getId()) { | ||
case R.id.content: | ||
|
||
new Task().execute("http://img.szhome.com/images/sznews/2012/07/20120703105442315.JPG"); | ||
break; | ||
|
||
case R.id.image: | ||
|
||
SavaImage(bitmap, Environment.getExternalStorageDirectory().getPath()+"/Test"); | ||
Toast.makeText(getBaseContext(), "图片保存", Toast.LENGTH_SHORT).show(); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
Handler handler=new Handler(){ | ||
public void handleMessage(android.os.Message msg) { | ||
if(msg.what==0x123){ | ||
image.setImageBitmap(bitmap); | ||
} | ||
}; | ||
}; | ||
|
||
|
||
class Task extends AsyncTask<String, Integer, Void>{ | ||
|
||
protected Void doInBackground(String... params) { | ||
bitmap=GetImageInputStream((String)params[0]); | ||
return null; | ||
} | ||
|
||
protected void onPostExecute(Void result) { | ||
super.onPostExecute(result); | ||
Message message=new Message(); | ||
message.what=0x123; | ||
handler.sendMessage(message); | ||
} | ||
|
||
} | ||
|
||
public void SavaImage(Bitmap bitmap, String path){ | ||
File file=new File(path); | ||
FileOutputStream fileOutputStream=null; | ||
if(!file.exists()){ | ||
file.mkdir(); | ||
} | ||
try { | ||
fileOutputStream=new FileOutputStream(path+"/"+System.currentTimeMillis()+".png"); | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,fileOutputStream); | ||
fileOutputStream.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
com1614080901140/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,33 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="edu.hzuapps.androidlabs.com1614080901140.ThirdActivity" | ||
android:orientation="vertical" | ||
> | ||
|
||
<ImageView | ||
android:id="@+id/image" | ||
android:layout_width="fill_parent" | ||
android:layout_height="450dp" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentTop="true" | ||
android:layout_marginTop="22dp" /> | ||
|
||
<Button | ||
android:id="@+id/content" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentBottom="true" | ||
android:layout_alignParentEnd="true" | ||
android:layout_alignParentRight="true" | ||
android:layout_marginBottom="20dp" | ||
android:layout_marginEnd="68dp" | ||
android:layout_marginRight="68dp" | ||
android:background="#ff8c00" | ||
android:text="路线查看" /> | ||
|
||
</RelativeLayout> | ||
|
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,25 @@ | ||
实验六 | ||
|
||
一.实验目的 | ||
掌握Android网络访问方法; | ||
|
||
二.实验内容 | ||
从网络下载一个文件(图片、MP3、MP4); | ||
保存到手机,在应用中使用文件; | ||
将应用运行结果截图; | ||
|
||
三.实验步骤 | ||
1.修改AndroidManifest.xml文件,获取连接网络的权限; | ||
2.重新建立一个activity文件,在相应的xml文件中加入button,点击就会下载相应的图片; | ||
3.实现图片的下载 | ||
|
||
四.实验截图 | ||
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.2.png) | ||
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.1.png) | ||
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.0.png) | ||
|
||
|
||
五.实验体会 | ||
这次实验的难点在于设置app访问网络的权限以及下载图片的功能实现,刚开始时输入几个图片链接发现在apk文件上都下载不下来还以为是代码出错, | ||
后来再重试几次发现是图片问题,有的图片能下载成功,有一些则不能,虽然现在还搞不懂是什么原因造成的,但在以后的自主学习中我相信这些问题都会被解决的。 | ||
|