调用系统内置的gallery查看图片

本文介绍了一种在Android应用中实现图片选择器的方法。通过发送一个意图(Intent)来调用系统的文件选择器,并指定要获取的文件类型为SD卡上的图片文件。在用户选择图片后,通过 onActivityResult 方法获取所选图片的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
// 告诉打开的内容

intent.setType("file:///sdcard/image/*");  
startActivityForResult(intent, 1); 

 

protected final void onActivityResult(final int requestCode, final int 
                     resultCode, final Intent i) { 
    super.onActivityResult(requestCode, resultCode, i); 
 
  // this matches the request code in the above call 
  if (requestCode == 1) { 
      Uri _uri = i.getData(); 
 
    // this will be null if no image was selected... 
    if (_uri != null) { 
      // now we get the path to the image file 
     cursor = getContentResolver().query(_uri, null, 
                                      null, null, null); 
     cursor.moveToFirst(); 
     String imageFilePath = cursor.getString(0); 
     cursor.close(); 
     } 
   } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值