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
unknown
committed
May 25, 2018
1 parent
a3f1a6f
commit fcaad34
Showing
7 changed files
with
112 additions
and
10 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
soft1614080902204/app/src/main/java/com/example/gwl20/soft1614080902204/HttpThread.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,70 @@ | ||
package com.example.gwl20.soft1614080902204; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.widget.ImageView; | ||
import android.content.Context; | ||
import android.os.Environment; | ||
|
||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
public class HttpThread extends Thread { | ||
private String url; | ||
private ImageView imageView; | ||
private Handler handler; | ||
public HttpThread(String url, ImageView imageView,Handler handler){ | ||
this.url=url; | ||
this.imageView=imageView; | ||
this.handler=handler; | ||
} | ||
@Override | ||
public void run(){ | ||
try { | ||
URL httpUrl=new URL(url); | ||
HttpURLConnection conn=(HttpURLConnection)httpUrl.openConnection(); | ||
conn.setReadTimeout(5000); | ||
conn.setRequestMethod("GET"); | ||
conn.setDoInput(true); | ||
InputStream in=conn.getInputStream(); | ||
FileOutputStream out=null; | ||
File downloadFile=null; | ||
String fileName=String.valueOf(System.currentTimeMillis()); | ||
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ | ||
File appDir=Environment.getExternalStorageDirectory(); | ||
downloadFile=new File(appDir,fileName); | ||
out=new FileOutputStream(downloadFile); | ||
} | ||
// String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo"; | ||
// File appDir = new File(storePath); | ||
// if (!appDir.exists()) { | ||
// appDir.mkdir(); } | ||
// downloadFile=new File(appDir,fileName); | ||
// out=new FileOutputStream(downloadFile); | ||
|
||
byte [] b=new byte[4*1024]; | ||
int len; | ||
if(out!=null) { | ||
while ((len = in.read(b)) != -1) { | ||
out.write(b,0,len); | ||
} | ||
} | ||
final Bitmap bitmap= BitmapFactory.decodeFile(downloadFile.getAbsolutePath()); | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
}); | ||
} catch (java.io.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
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
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
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.