这里,要把选择 和 截剪后的图片存到sdcard 上的指目录中,那么就要有操作sdcard 的权限
- <!-- 往sdcard中写入数据的权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <!-- 在sdcard中创建/删除文件的权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往sdcard中写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 在sdcard中创建/删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
然后在activity 调用
- 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 对应
- }
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 对应
}
返回数据
- //调用成功反回方法
- @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;
- }
- }
//调用成功反回方法
@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 version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@color/white"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/showSelectImageId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="onclickFun"
- android:text="选择图片" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/showSelectImageId" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclickFun" android:text="选择图片" /> </LinearLayout>
2、Activity 类
- 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;
- }
- }
- }
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 加上权限
- <!-- 往sdcard中写入数据的权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <!-- 在sdcard中创建/删除文件的权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往sdcard中写入数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 在sdcard中创建/删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />