AndroidStudio——网络图片的读取与保存
2016-08-21 16:48
344 查看
SaveImageViewActivity:
activity_save_image_view布局文件
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>
相关文章推荐
- android 读取本地或网络图片,转换为Bitmap 并保存本地中
- Android数据存储:获取网络图片把图片保存到SD卡中并从SDk卡中读取(通过Bitmap方法)
- Android对图片的压缩读取和保存
- android读取本地或网络图片方法
- android-读取Assets图片资源保存到SD - 随心
- Androidr的图片的缩放、读取、压缩保存
- Android网络:开发浏览器(五)——功能完善之保存图片实现
- Android手机开发:从网络上读取图片
- 【转】android:网络图片转为bitmap 保存至SD卡中
- android-读取Assets图片资源保存到SD - 随心
- 第101章、读取网络图片(从零开始学Android)
- android通过url读取网络图片
- Android开发技术:Android对图片的压缩读取和保存
- Java从网络读取图片并保存至本地 .
- 使用程序保存网页中的图片 网络读取图片
- Android手机开发:从网络上读取图片
- Androidr的图片的缩放、读取、压缩保存
- android读取网络上的图片
- android 网络图片与网页读取
- Android网络:开发浏览器(五)——功能完善之保存图片实现