SaveImageViewActivity:
public class SaveImageViewActivity extends AppCompatActivity {
private ImageView img, showImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_image_view);
img = (ImageView) findViewById(R.id.img);
showImg = (ImageView) findViewById(R.id.showImg);
}
public void readImg(View v) {
String path = Environment.getExternalStorageDirectory() + "/1.png";
//方法1:根據URI 加載圖片
//showImg.setImageURI(Uri.parse(path));
/*
方法2:通過BitmapFactory 的靜態方法decodeFile()
參數爲圖片路徑
*/
Bitmap bitmap = BitmapFactory.decodeFile(path);
showImg.setImageBitmap(bitmap);
/*
方法3:通過BitmapFactory 的靜態方法decodeStream()
參數爲 輸入流 InputStream
try {
BitmapFactory.decodeStream(new FileInputStream(path));
showImg.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
}
//佈局監聽 參數View v一定要寫
//保存圖片到SD卡
public void saveImg(View v) {
//獲取BitmapDrawable對象
BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
/*
通過Bitmap(位圖)壓縮的方法(compress)保存圖片到SD卡
參數1:圖片格式(PNG JPEJ WEBP)
參數2:圖片質量(0-100)
參數3:輸出流
*/
//取得SD卡根目錄
File root = Environment.getExternalStorageDirectory();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(root + "/1.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//從網絡獲取圖片
public void getUrlImg(View v) {
new GetImg().execute("http://img2.imgtn.bdimg.com/it/u=169341381,1292926655&fm=21&gp=0.jpg");
}
public void saveHttpImg(View v){
new SaveHttpImg().execute("http://img2.imgtn.bdimg.com/it/u=169341381,1292926655&fm=21&gp=0.jpg");
}
public class GetImg extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
HttpURLConnection con = null;
//拿數據
InputStream is = null;
try {
URL url=new URL(params[0]);
con= (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5*1000);
if (con.getResponseCode()==200){
is=con.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (con!=null){
con.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
showImg.setImageBitmap(bitmap);
}
}
public class SaveHttpImg extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
HttpURLConnection con = null;
InputStream is = null;
try {
URL url = new URL(strings[0]);
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5*1000);
con.setReadTimeout(5*1000);
File root = Environment.getExternalStorageDirectory();
FileOutputStream fos = new FileOutputStream(root+"/http.jpg");
if(con.getResponseCode()==200){
is = con.getInputStream();
int next=0;
byte[] bytes = new byte[1024];
while ( (next = is.read(bytes))>0){
fos.write(bytes,0,next);
}
fos.flush();
fos.close();
return root+"/http.jpg";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con!=null){
con.disconnect();
}
}
return "";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(!s.equals("")){
Toast.makeText(SaveImageViewActivity.this, "保存路徑:" + s, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SaveImageViewActivity.this,"保存失敗:",Toast.LENGTH_SHORT).show();
}
}
}
}
activity_save_image_view佈局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dell.jreduch008.SaveImageViewActivity">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:orientation="horizontal">
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/save"
android:layout_weight="1"
android:onClick="readImg"
android:text="讀取圖片" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_weight="1"
android:onClick="saveImg"
android:text="保存圖片" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll1"
android:orientation="horizontal">
<Button
android:id="@+id/readUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_weight="1"
android:onClick="getUrlImg"
android:text="獲取網絡圖片"/>
<Button
android:id="@+id/saveImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_weight="1"
android:onClick="saveHttpImg"
android:text="保存網絡圖片"/>
</LinearLayout>
<ImageView
android:id="@+id/showImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/ll2" />
</RelativeLayout>