android获取imageView图片并保存

网上看的很多雷同,试问在转载时有多少人试过可以用吗?

最反感那样的博文,

自己亲测可用,

package com.example.deletefile_1;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ImageView imageView1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView1=(ImageView)findViewById(R.id.imageView1);

		imageView1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				imageView1.buildDrawingCache(true);  
				imageView1.buildDrawingCache();  
				  Bitmap bitmap = imageView1.getDrawingCache();  
				  saveBitmapFile(bitmap);
				imageView1.setDrawingCacheEnabled(false);
				Toast.makeText(MainActivity.this, "点击", Toast.LENGTH_SHORT).show();
				
			}
		});
		
	}
	////////////////////////
	public void saveBitmapFile(Bitmap bitmap){
		
		 File temp = new File("/sdcard/1delete/");//要保存文件先创建文件夹   
		 if (!temp.exists()) {
		 temp.mkdir();
		 }
		////重复保存时,覆盖原同名图片
        File file=new File("/sdcard/1delete/1.jpg");//将要保存图片的路径和图片名称
    //    File file =  new File("/sdcard/1delete/1.png");/////延时较长
		 try {
			 BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream(file));
			 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
			 bos.flush();
			 bos.close();
			 
        } catch (IOException e) {
                e.printStackTrace();
        }
}
	
	public void deleteFile(View v){////点击按钮删除这个文件
		File file = new File("/sdcard/1spray/1.png");
		if(file.exists()){
			file.delete();
		}
		
	}///deleteFile
}


    

    



    

    
     
    
    
        
            
                

                
            
        
    







你可能感兴趣的:(Android)