Skip to content

Commit

Permalink
hzuapps#6 hzuapps#1024 第六次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhuangj committed May 27, 2018
1 parent 46d11f7 commit b78b44b
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
Binary file added soft1614080902413/10.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 soft1614080902413/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions soft1614080902413/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.androidlabs.soft1614080902413">

<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=".InternetActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_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" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package edu.androidlabs.soft1614080902413;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class InternetActivity extends Activity {

private EditText editText;
private Button button;
private ImageView imageView;

private Bitmap bitmap;
//手柄更新的作用
Handler handler=new Handler(){
public void handleMessage(Message msg) {
if(msg.what==111){
imageView.setImageBitmap(bitmap);
}
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internet);

//初始化组件
editText=(EditText) findViewById(R.id.imagepath);
button=(Button) findViewById(R.id.upload);
imageView=(ImageView) findViewById(R.id.imageView);

//给下载按钮添加一个监听
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(t).start();//启动线程t
}
});
}

//为了下载图片资源,开辟一个新的子线程,线程名字为t
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();

//再一次打开
inputStream=url.openStream();
File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/");
FileOutputStream fileOutputStream=new FileOutputStream(file);
int hasRead=0;
while((hasRead=inputStream.read())!=-1){
fileOutputStream.write(hasRead);
}
fileOutputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
};

}
28 changes: 28 additions & 0 deletions soft1614080902413/res/layout/activity_internet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<EditText
android:id="@+id/imagepath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入图片url地址"
android:textColor="#AAAAAA"
/>
<Button
android:id="@+id/upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击查看图片"
android:textColor="#1A2DED"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

0 comments on commit b78b44b

Please sign in to comment.