diff --git a/soft1614080902442/app/java/soft1614080902442main5Activity.java b/soft1614080902442/app/java/soft1614080902442main5Activity.java new file mode 100644 index 000000000..6f1503ee0 --- /dev/null +++ b/soft1614080902442/app/java/soft1614080902442main5Activity.java @@ -0,0 +1,88 @@ +package edu.hzuapps.androidlabs.soft1614080902442; +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 soft1614080902442main5Activity 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_soft1614080902442main5); + + //初始化组件 + 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(); + } + }; + }; + +} \ No newline at end of file diff --git a/soft1614080902442/app/mainfests/AndroidManifest.xml b/soft1614080902442/app/mainfests/AndroidManifest.xml index cdb31c4e8..d15907e1f 100644 --- a/soft1614080902442/app/mainfests/AndroidManifest.xml +++ b/soft1614080902442/app/mainfests/AndroidManifest.xml @@ -9,21 +9,19 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + - - + + + + + + diff --git a/soft1614080902442/app/res/layout/activity_soft1614080902442main5.xml b/soft1614080902442/app/res/layout/activity_soft1614080902442main5.xml new file mode 100644 index 000000000..807e691e6 --- /dev/null +++ b/soft1614080902442/app/res/layout/activity_soft1614080902442main5.xml @@ -0,0 +1,35 @@ + + + + + + +