Android获取SD卡中选中图片的路径(URL)示例
程序员文章站
2023-01-02 08:58:46
最近在做一个图片上传的功能,需要提供上传图片在sd卡中的路径,在网上看了些例子,改改调试成功,代码很简单。其布局文件如下: 复制代码 代码如下:
最近在做一个图片上传的功能,需要提供上传图片在sd卡中的路径,在网上看了些例子,改改调试成功,代码很简单。其布局文件如下:
<?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:orientation="vertical" >
<button
android:id="@+id/select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择sd卡中的图片"
/>
</linearlayout>
java文件如下:
package com.lostinai;
import java.io.ioexception;
import android.app.activity;
import android.content.contentresolver;
import android.content.intent;
import android.database.cursor;
import android.graphics.bitmap;
import android.net.uri;
import android.os.bundle;
import android.provider.mediastore;
import android.util.log;
import android.view.view;
import android.widget.button;
public class querypictureurlactivity extends activity {
private button select;
private final string image_type = "image/*";
private final int image_code = 0;
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
select = (button)findviewbyid(r.id.select);
select.setonclicklistener(new view.onclicklistener(){
public void onclick(view v) {
intent getalbum = new intent(intent.action_get_content);
getalbum.settype(image_type);
startactivityforresult(getalbum, image_code);
}
});
}
protected void onactivityresult(int requestcode, int resultcode, intent data){
if (resultcode != result_ok) { //此处的 result_ok 是系统自定义得一个常量
// log.e(tag,"activityresult resultcode error");
return;
}
bitmap bm = null;
contentresolver resolver = getcontentresolver();
if (requestcode == image_code) {
try {
uri originaluri = data.getdata(); //获得图片的uri
bm = mediastore.images.media.getbitmap(resolver, originaluri); //显得到bitmap图片
// 这里开始的第二部分,获取图片的路径:
string[] proj = {mediastore.images.media.data};
cursor cursor = managedquery(originaluri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data);
cursor.movetofirst();
//最后根据索引值获取图片路径
string path = cursor.getstring(column_index);
log.e("lostinai",path);
}catch (ioexception e) {
log.e("lostinai",e.tostring());
}
}
}
}
最后别忘记了加上权限啊
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="android.permission.mount_unmount_filesystems" />
<uses-permission android:name="android.permission.write_external_storage" />
复制代码 代码如下:
<?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:orientation="vertical" >
<button
android:id="@+id/select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择sd卡中的图片"
/>
</linearlayout>
java文件如下:
复制代码 代码如下:
package com.lostinai;
import java.io.ioexception;
import android.app.activity;
import android.content.contentresolver;
import android.content.intent;
import android.database.cursor;
import android.graphics.bitmap;
import android.net.uri;
import android.os.bundle;
import android.provider.mediastore;
import android.util.log;
import android.view.view;
import android.widget.button;
public class querypictureurlactivity extends activity {
private button select;
private final string image_type = "image/*";
private final int image_code = 0;
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
select = (button)findviewbyid(r.id.select);
select.setonclicklistener(new view.onclicklistener(){
public void onclick(view v) {
intent getalbum = new intent(intent.action_get_content);
getalbum.settype(image_type);
startactivityforresult(getalbum, image_code);
}
});
}
protected void onactivityresult(int requestcode, int resultcode, intent data){
if (resultcode != result_ok) { //此处的 result_ok 是系统自定义得一个常量
// log.e(tag,"activityresult resultcode error");
return;
}
bitmap bm = null;
contentresolver resolver = getcontentresolver();
if (requestcode == image_code) {
try {
uri originaluri = data.getdata(); //获得图片的uri
bm = mediastore.images.media.getbitmap(resolver, originaluri); //显得到bitmap图片
// 这里开始的第二部分,获取图片的路径:
string[] proj = {mediastore.images.media.data};
cursor cursor = managedquery(originaluri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data);
cursor.movetofirst();
//最后根据索引值获取图片路径
string path = cursor.getstring(column_index);
log.e("lostinai",path);
}catch (ioexception e) {
log.e("lostinai",e.tostring());
}
}
}
}
最后别忘记了加上权限啊
复制代码 代码如下:
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="android.permission.mount_unmount_filesystems" />
<uses-permission android:name="android.permission.write_external_storage" />