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
Showing
5 changed files
with
143 additions
and
0 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.
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,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> |
88 changes: 88 additions & 0 deletions
88
soft1614080902413/java/edu/androidlabs/soft1614080902413/InternetActivity.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,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(); | ||
} | ||
}; | ||
}; | ||
|
||
} |
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,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> |