android打开,保存图片到sd卡,显示图片

1.打开根目录下test.jpg

 Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.jpg");

 int[] pixels = new int[bm.getWidth()*bm.getHeight()];

 bm.getPixels =(pixels,0,bm.getWidth(),0,0,bm.getWidth(),bm.getHeight() );

 Bitmap bm1 = bm.copy(bm.getConfig(),true);//bm is not Mutable ,像素值不能改

 bm1.setPixels(pixels,0,bm.getWidth(),0,0,bm.getWidth(),bm.getHeight() );

 saveBitmap("test2",bm1);

  ImageVie imgview = (ImageView)findViewById(R.id.imageView1);

  imgview.setImageBitmap(bm1);

 

2.保存图片

  public void saveBitmap(String name,Bitmap mBitmap){

 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name+".png");

file.createNewFile();

 FileOutPutStream fout = null;

 fout = new FileOutPutStream(file);

mBitmap.compress(Bitmap.CompressFormat.PNG,100,fout);

fout.flush();

fout.close();

}

 

你可能感兴趣的:(android)