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
}
<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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:onClick="deleteFile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="70dp"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/button1"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.deletefile_1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>





你可能感兴趣的:(imageview,保存,获取图片)