android 选择本地图片并截剪图片保存到,sdcard指定目录中

这里,要把选择 和 截剪后的图片存到sdcard 上的指目录中,那么就要有操作sdcard 权限

 

Xml代码 复制代码  收藏代码
  1. <!-- 往sdcard中写入数据的权限 -->  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  3.   
  4. <!-- 在sdcard中创建/删除文件的权限 -->  
  5. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

 

然后在activity 调用

Java代码 复制代码  收藏代码
  1.     public void onclickFun(View view) {   
  2.         Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);   
  3.         innerIntent.putExtra("crop""true");// 才能出剪辑的小方框,不然没有剪辑功能,只能选取图片   
  4.         innerIntent.putExtra("aspectX"1); // 出现放大和缩小   
  5.         innerIntent.setType("image/*"); // 查看类型 详细的类型在 com.google.android.mms.ContentType    
  6.            
  7.   
  8. //===============================   
  9. //                       innerIntent.setType("image/*");    
  10. //          innerIntent.putExtra("crop", "true");      
  11. //          innerIntent.putExtra("aspectX", 1);//裁剪框比例     
  12. //          innerIntent.putExtra("aspectY", 1);     
  13. //          innerIntent.putExtra("outputX", 120);//输出图片大小     
  14. //          innerIntent.putExtra("outputY", 120);     
  15. //================================   
  16.  tempFile=new File("/sdcard/ll1x/"+Calendar.getInstance().getTimeInMillis()+".jpg"); // 以时间秒为文件名   
  17.         File temp = new File("/sdcard/ll1x/");//自已项目 文件夹   
  18.         if (!temp.exists()) {   
  19.             temp.mkdir();   
  20.         }   
  21.         innerIntent.putExtra("output", Uri.fromFile(tempFile));  // 专入目标文件      
  22.         innerIntent.putExtra("outputFormat""JPEG"); //输入文件格式     
  23.            
  24.         Intent wrapperIntent = Intent.createChooser(innerIntent, "先择图片"); //开始 并设置标题   
  25.         startActivityForResult(wrapperIntent, 1); // 设返回 码为 1  onActivityResult 中的 requestCode 对应   
  26.     }  
	public void onclickFun(View view) {
		Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
		innerIntent.putExtra("crop", "true");// 才能出剪辑的小方框,不然没有剪辑功能,只能选取图片
		innerIntent.putExtra("aspectX", 1); // 出现放大和缩小
		innerIntent.setType("image/*"); // 查看类型 详细的类型在 com.google.android.mms.ContentType 
		

//===============================
//                       innerIntent.setType("image/*"); 
//			innerIntent.putExtra("crop", "true");   
//			innerIntent.putExtra("aspectX", 1);//裁剪框比例  
//			innerIntent.putExtra("aspectY", 1);  
//			innerIntent.putExtra("outputX", 120);//输出图片大小  
//			innerIntent.putExtra("outputY", 120);  
//================================
 tempFile=new File("/sdcard/ll1x/"+Calendar.getInstance().getTimeInMillis()+".jpg"); // 以时间秒为文件名
		File temp = new File("/sdcard/ll1x/");//自已项目 文件夹
		if (!temp.exists()) {
			temp.mkdir();
		}
		innerIntent.putExtra("output", Uri.fromFile(tempFile));  // 专入目标文件   
		innerIntent.putExtra("outputFormat", "JPEG"); //输入文件格式  
		
		Intent wrapperIntent = Intent.createChooser(innerIntent, "先择图片"); //开始 并设置标题
		startActivityForResult(wrapperIntent, 1); // 设返回 码为 1  onActivityResult 中的 requestCode 对应
	}

 

 

返回数据

Java代码 复制代码  收藏代码
  1. //调用成功反回方法   
  2.     @Override  
  3.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  4.         super.onActivityResult(requestCode, resultCode, data);   
  5.         switch (requestCode) {   
  6.         case 1:   
  7.             imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));   
  8.             break;   
  9.         }   
  10.     }  
//调用成功反回方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
        case 1:
        	imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
        	break;
		}
	}
 

 

添加上一个 例子:

 

1、layout 文件  get_images_view.xml

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@color/white"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <ImageView  
  10.         android:id="@+id/showSelectImageId"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content" />  
  13.   
  14.     <Button  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:onClick="onclickFun"  
  18.         android:text="选择图片" />  
  19.   
  20. </LinearLayout>  
 

2、Activity 类

Java代码 复制代码  收藏代码
  1. package com.main;   
  2.   
  3. import java.io.File;   
  4. import java.util.Calendar;   
  5.   
  6. import android.app.Activity;   
  7. import android.content.Intent;   
  8. import android.graphics.drawable.Drawable;   
  9. import android.net.Uri;   
  10. import android.os.Bundle;   
  11. import android.view.View;   
  12. import android.widget.ImageView;   
  13.   
  14. public class GetImagesActivity extends Activity {   
  15.     private ImageView imageView;   
  16.     private File tempFile;   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {   
  19.         super.onCreate(savedInstanceState);   
  20.         setContentView(R.layout.get_images_view);   
  21.         imageView = (ImageView) findViewById(R.id.showSelectImageId);   
  22.     }   
  23.   
  24.     public void onclickFun(View view) {   
  25.         Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);   
  26.         innerIntent.putExtra("crop""true");// 才能出剪辑的小方框,不然没有剪辑功能,只能选取图片   
  27.         innerIntent.putExtra("aspectX"1); // 出现放大和缩小   
  28.         innerIntent.setType("image/*"); // 查看类型 详细的类型在 com.google.android.mms.ContentType    
  29.            
  30.         tempFile=new File("/sdcard/ll1x/"+Calendar.getInstance().getTimeInMillis()+".jpg"); // 以时间秒为文件名   
  31.         File temp = new File("/sdcard/ll1x/");//自已项目 文件夹   
  32.         if (!temp.exists()) {   
  33.             temp.mkdir();   
  34.         }   
  35.         innerIntent.putExtra("output", Uri.fromFile(tempFile));  // 专入目标文件      
  36.         innerIntent.putExtra("outputFormat""JPEG"); //输入文件格式     
  37.            
  38.         Intent wrapperIntent = Intent.createChooser(innerIntent, "先择图片"); //开始 并设置标题   
  39.         startActivityForResult(wrapperIntent, 1); // 设返回 码为 1  onActivityResult 中的 requestCode 对应   
  40.     }   
  41.        
  42.     //调用成功反回方法   
  43.     @Override  
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  45.         super.onActivityResult(requestCode, resultCode, data);   
  46.         switch (requestCode) {   
  47.         case 1:   
  48.             imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));   
  49.             break;   
  50.         }   
  51.     }   
  52. }  
package com.main;

import java.io.File;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class GetImagesActivity extends Activity {
	private ImageView imageView;
	private File tempFile;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_images_view);
		imageView = (ImageView) findViewById(R.id.showSelectImageId);
	}

	public void onclickFun(View view) {
		Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
		innerIntent.putExtra("crop", "true");// 才能出剪辑的小方框,不然没有剪辑功能,只能选取图片
		innerIntent.putExtra("aspectX", 1); // 出现放大和缩小
		innerIntent.setType("image/*"); // 查看类型 详细的类型在 com.google.android.mms.ContentType 
		
		tempFile=new File("/sdcard/ll1x/"+Calendar.getInstance().getTimeInMillis()+".jpg"); // 以时间秒为文件名
		File temp = new File("/sdcard/ll1x/");//自已项目 文件夹
		if (!temp.exists()) {
			temp.mkdir();
		}
		innerIntent.putExtra("output", Uri.fromFile(tempFile));  // 专入目标文件   
		innerIntent.putExtra("outputFormat", "JPEG"); //输入文件格式  
		
		Intent wrapperIntent = Intent.createChooser(innerIntent, "先择图片"); //开始 并设置标题
		startActivityForResult(wrapperIntent, 1); // 设返回 码为 1  onActivityResult 中的 requestCode 对应
	}
	
	//调用成功反回方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
        case 1:
        	imageView.setImageDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
        	break;
		}
	}
}
 

 

3、 AndroidManifest.xml 加上权限

 

Xml代码 复制代码  收藏代码
  1. <!-- 往sdcard中写入数据的权限 -->  
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  3.   
  4.     <!-- 在sdcard中创建/删除文件的权限 -->  
  5.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值