Skip to content

Commit

Permalink
Merge pull request #2674 from dl3213/master
Browse files Browse the repository at this point in the history
#6 #910 lab6
  • Loading branch information
zengsn authored Jun 6, 2018
2 parents 691ab56 + 7793c74 commit c604edf
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 409 deletions.
6 changes: 5 additions & 1 deletion soft1614080902201/lab6/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,170 +1,114 @@
package dl.soft1614080902201;

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.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;
import android.graphics.*;
import android.os.Handler;
import android.os.Message;
import java.io.File;
import android.os.AsyncTask;
import android.os.Environment;

import dl.soft1614080902201.R;
import dl.soft1614080902201.*;

public class NetworkActivity extends Activity implements View.OnClickListener{

public static final String TAG = NetworkActivity.class.getSimpleName();
public static final String WEB = "http://www.bing.com";
public static final String IMAGE_URL_PREFIX = "https://raw.githubusercontent.com/hzuapps/android-labs/master/app/src/main/res/drawable/";

static String[] imageNames = {"image_bmp.png", "image_gif.png", "image_ico.png",
"image_jpeg.png", "image_png.png", "image_tiff.png"};

public class NetworkActivity extends AppCompatActivity implements OnClickListener{

private Button mDownloadImageButton;
private ImageView mImageshow;
private TextView mNetworkText;
private TextView mResultText;
Button download;
ImageView image;
Bitmap bitmap;

private boolean mConnected;
private NetworkFileDownloader mFileDownloader;
private File mPrivateRootDir;
private File mImagesDir;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network);
download=(Button)findViewById(R.id.button_Download);
image=(ImageView)findViewById(R.id.image_show);

mDownloadImageButton = (Button) findViewById(R.id.button_download_image);
mImageshow =(ImageView)findViewById(R.id.text_show);
mNetworkText = (TextView) findViewById(R.id.text_network);
mResultText = (TextView) findViewById(R.id.text_result);
mDownloadImageButton.setOnClickListener(this);
mPrivateRootDir = getFilesDir();
mImagesDir = new File(mPrivateRootDir, "images");
download.setOnClickListener(this);;
image.setOnClickListener(this);;
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button_download_image) {
downloadImages();

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;
}

private void downloadImages() {
// 创建下载器
mFileDownloader = new NetworkFileDownloader(new NetworkFileDownloader.OnImageDownloadListener() {
@Override
public void onError(String error) {
Toast.makeText(NetworkActivity.this, error, Toast.LENGTH_LONG).show();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_Download:

@Override
public void onProgressChange(int percent) {
Log.i(TAG, "当前进度 = " + percent);
}
new Task().execute("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528041114167&di=8d85340b9dfc1d48c449e291f9a6638f&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0117e2571b8b246ac72538120dd8a4.jpg");
break;

@Override
public void onComplete(Bitmap bitmap, String imageUrl) {
// 下载的图片格式为PNG
final Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG;
// 解析出原始文件名
String filename = imageUrl.replace(IMAGE_URL_PREFIX, "");
final File imageFile = new File(mImagesDir, filename);
// 将文件保存到磁盘中
NetworkFileDownloader.writeToDisk(imageFile, bitmap, new NetworkFileDownloader.OnBitmapSaveListener() {
@Override
public void onBitmapSaved() {
Toast.makeText(NetworkActivity.this, "文件已保存: " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
@Override
public void onBitmapSaveError(String error) {
Toast.makeText(NetworkActivity.this, error, Toast.LENGTH_LONG).show();
}
}, format, false);
}
});
case R.id.image_show:

SavaImage(bitmap, Environment.getExternalStorageDirectory().getPath()+"/Test");
Toast.makeText(getBaseContext(), "图片保存", Toast.LENGTH_SHORT).show();
break;

// 下载所有文件
for(String imageName :imageNames) {
String imageUrl = IMAGE_URL_PREFIX + imageName;
mFileDownloader.download(imageUrl, true);
default:
break;
}
}

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;
}

Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0x123){
mImageshow.setImageBitmap(bitmap);
}
};
if(msg.what==0x123){
image.setImageBitmap(bitmap);
}
};
};


class Task extends AsyncTask<String, Integer, Void>{
class Task extends AsyncTask<String, Integer, Void>{

protected Void doInBackground(String... params) {
bitmap=GetImageInputStream((String)params[0]);
return null;
}
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);
}
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();
}
}
}
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();
}
}
}
Loading

0 comments on commit c604edf

Please sign in to comment.