android从相册中获取图片

跳转Activity,调用系统照相机软件拍照获取照片:

<span style="white-space:pre">	</span>Intent intent_gocamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
<span style="white-space:pre">			</span>/* 使用当前时间作为新拍的照片的文件名 */
<span style="white-space:pre">			</span>Calendar c = Calendar.getInstance();<span style="white-space:pre">	</span>// 获取当前时间状态
<span style="white-space:pre">			</span>String currentDate = c.get(Calendar.YEAR) + ""
<span style="white-space:pre">					</span>+ (c.get(Calendar.MINUTE) + 1) + ""
<span style="white-space:pre">					</span>+ c.get(Calendar.DAY_OF_MONTH) + ""
<span style="white-space:pre">					</span>+ c.get(Calendar.HOUR_OF_DAY) + "" + c.get(Calendar.MINUTE)
<span style="white-space:pre">					</span>+ "" + c.get(Calendar.SECOND);
<span style="white-space:pre">			</span>catchPicName_camera = "WD" + currentDate + ".jpg"; // 拍照保存的图片以.jpg为后缀名
<span style="white-space:pre">			</span>File filedir = new File(SettingsUtilsTools.getSDPath()+catchPicStoreDir);
<span style="white-space:pre">			</span>if(!filedir.exists()) {
<span style="white-space:pre">				</span>filedir.mkdirs();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>File file_catchPic = new File(getSDPath()+catchPicStoreDir, catchPicName_camera);<span style="white-space:pre">	</span>// 使用<span style="font-family: Arial, Helvetica, sans-serif;">catchPicStoreDir为文件夹名,如“/myimages/”</span>

<span style="white-space:pre">			</span>Uri uri = Uri.fromFile(file_catchPic);
//<span style="white-space:pre">			</span>intent01.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
<span style="white-space:pre">			</span>intent_gocamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);<span style="white-space:pre">	</span>// 通知照相机将拍照所得的照片存储在该 uri中,若不将拍照的结果存储,则在结果中获取到的回事缩略图
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>startActivityForResult(intent_gocamera, CASE_CAMERA);
			


跳转Activity,调用系统的图库浏览器选择图片:

<span style="white-space:pre">	</span>/* 从图库中选择图片 */
	Intent intent02 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// ACTION_OPEN_DOCUMENT
	startActivityForResult(intent02, SELECT_PIC);<span style="white-space:pre">	</span>// SELECT_PIC的值为启动有返回值的Activity所需的requestCode,可任意自定义数值
在onActivityResult方法中获取返回的图片数据,并将图片传递到另一个Activity中:

<span style="white-space:pre">		</span>Intent intent_godispose = new Intent("com.go.action.PICHANDLE");
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>switch (requestCode) {
<span style="white-space:pre">		</span>case SELECT_PIC:
<span style="white-space:pre">			</span>Uri uri_pic_back_photo = data.getData();
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>Bitmap bitmap01 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri_pic_back_photo);
<span style="white-space:pre">				</span>
<span style="white-space:pre">				</span>String[] proj = {MediaStore.Images.Media.DATA};
<span style="white-space:pre">				</span>// 好像是android多媒体数据库的封装接口,具体的看Android文档
<span style="white-space:pre">				</span>Cursor cursor = managedQuery(uri_pic_back_photo, proj, null, null, null);
<span style="white-space:pre">				</span>// 按我个人理解 这个是获得用户选择的图片的索引值
<span style="white-space:pre">				</span>int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
<span style="white-space:pre">				</span>// 将光标移至开头 ,这个很重要,不小心很容易引起越界
<span style="white-space:pre">				</span>cursor.moveToFirst();
<span style="white-space:pre">				</span>// 最后根据索引值获取图片路径
<span style="white-space:pre">				</span>String path = cursor.getString(column_index);
<span style="white-space:pre">				</span>String[] pic_ = path.split("/");
<span style="white-space:pre">				</span>String pic_name = pic_[pic_.length-1];


<span style="white-space:pre">				</span>Log.e("==>> TAG <<==", path);
<span style="white-space:pre">				</span>
<span style="white-space:pre">				</span>intent_godispose.putExtra(TAG_PICPATH, path);
<span style="white-space:pre">				</span>startActivity(intent_godispose);
<span style="white-space:pre">			</span>} catch (FileNotFoundException e) {
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>} catch (IOException e) {
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>case CASE_CAMERA:
<span style="white-space:pre">			</span>// Bitmap bitmap = (Bitmap) data.getExtras().get("data"); // 这样获取到的拍照结果是缩略,所以不用这个
<span style="white-space:pre">			</span>File file_catchPic = new File(SettingsUtilsTools.getSDPath() + catchPicStoreDir, catchPicName_camera);
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>String path_camera = file_catchPic.getAbsolutePath();
<span style="white-space:pre">			</span>Log.e("TAG ==>> 拍照结果文件存放路径", path_camera);


//<span style="white-space:pre">			</span>Intent intent11 = new Intent("com.go.action.PICHANDLE");
<span style="white-space:pre">			</span>intent_godispose.putExtra(TAG_PICPATH, path_camera);
<span style="white-space:pre">			</span>startActivity(intent_godispose);


<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>default:
<span style="white-space:pre">			</span>break;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>

其中工具类BitmapUtilsTools下的bitmap2bytes方法如下:

	
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * Bitmap转字节流
<span style="white-space:pre">	</span> * @param bmp
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static byte[] bitmap2bytes(Bitmap bmp, String pic_name) {
<span style="white-space:pre">		</span>ByteArrayOutputStream baos = new ByteArrayOutputStream();
<span style="white-space:pre">		</span>String[] args = pic_name.split("\\.");<span style="white-space:pre">	</span>// 这里注意不能直接使用 split("."),否则无法正确分割,正确的说是不会分割,应该是因为“.”是特殊字符的原因
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>String suffix = args[args.length-1];<span style="white-space:pre">	</span>// 获取后缀名名称
<span style="white-space:pre">		</span>if(suffix.equalsIgnoreCase("JPG")) {
<span style="white-space:pre">			</span>bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);<span style="white-space:pre">	</span>// compress()中第二个参数为0时压缩比例最大,100则不压缩
<span style="white-space:pre">		</span>} else if(suffix.equalsIgnoreCase("PNG")) {
<span style="white-space:pre">			</span>bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return baos.toByteArray();
<span style="white-space:pre">	</span>}
	
将图片存储到指定的SD卡目录下:

<span style="white-space:pre">		</span>Intent intent_godispose = new Intent("com.go.action.PICHANDLE");
		
		switch (requestCode) {
		case SELECT_PIC:
			Uri uri_pic_back_photo = data.getData();
			try {
				Bitmap bitmap01 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri_pic_back_photo);
				
				String[] proj = {MediaStore.Images.Media.DATA};
				// 好像是android多媒体数据库的封装接口,具体的看Android文档
				Cursor cursor = managedQuery(uri_pic_back_photo, proj, null, null, null);
				// 按我个人理解 这个是获得用户选择的图片的索引值
				int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				// 将光标移至开头 ,这个很重要,不小心很容易引起越界
				cursor.moveToFirst();
				// 最后根据索引值获取图片路径
				String path = cursor.getString(column_index);
				String[] pic_ = path.split("/");
				String pic_name = pic_[pic_.length-1];

				Log.e("==>> TAG <<==", path);
				
				intent_godispose.putExtra(TAG_PICPATH, path);<span style="white-space:pre">	</span>// 将图片的路径传到另一个Activity
				startActivity(intent_godispose);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			break;
		case CASE_CAMERA:
			// Bitmap bitmap = (Bitmap) data.getExtras().get("data"); // 获取拍照后的结果——缩略图
			File file_catchPic = new File(SettingsUtilsTools.getSDPath() + catchPicStoreDir, catchPicName_camera);
			
			String path_camera = file_catchPic.getAbsolutePath();
			Log.e("TAG ==>> 拍照结果文件存放路径", path_camera);

//			Intent intent11 = new Intent("com.go.action.PICHANDLE");
			intent_godispose.putExtra(TAG_PICPATH, path_camera);
			startActivity(intent_godispose);

			break;
		default:
			break;
		}


其中获取SD卡绝对路径的方法getSDPath()如下:

	
	/**
	 * 获取SD卡在手机上的路径
	 * @return
	 */
	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();
	}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio,从相册选择图片可以通过以下步骤实现: 1. 在布局文件添加一个Button和一个ImageView,用于选择图片和展示图片。 2. 在Activity获取Button和ImageView的实例,并为Button设置点击事件。 3. 在点击事件,使用Intent调用系统相册,并在onActivityResult方法获取选择的图片的Uri。 4. 将Uri转换为Bitmap,并设置到ImageView展示。 具体实现可以参考以下代码: 1. 在布局文件添加Button和ImageView: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/chooseLocalImage" android:text="点击选择本地照片" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/Image_message" android:layout_width="150dp" android:layout_height="150dp" /> </LinearLayout> ``` 2. 在Activity获取Button和ImageView的实例,并为Button设置点击事件: ``` public class MainActivity extends AppCompatActivity { private Button chooseLocalImage; private ImageView Image_message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chooseLocalImage = findViewById(R.id.chooseLocalImage); Image_message = findViewById(R.id.Image_message); chooseLocalImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); } }); } } ``` 3. 在onActivityResult方法获取选择的图片的Uri,并将Uri转换为Bitmap: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK && data != null) { Uri uri = data.getData(); try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); Image_message.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值