惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
A
About on SuperTechFans
U
Unit 42
MyScale Blog
MyScale Blog
J
Java Code Geeks
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
量子位
月光博客
月光博客
G
Google Developers Blog
V
V2EX
博客园 - 聂微东
宝玉的分享
宝玉的分享
IT之家
IT之家
Vercel News
Vercel News

博客园 - herry507

在将 nvarchar 值 '0.00000' 转换成数据类型 int 时失败。 excel VBA点击图片放大缩小 mssql 解析XML 特殊字符造成浏览器显示异常 asysc /wait task 示例执行 MSSQL中解析JSON特殊字符 直接执行与EXCU里执行,竟效果不同 占有率最高的工业总线:PROFINET、Modbus 与 EtherCAT nvarchar和varchar区别 Wappalyzer的分析和欺骗 mssql 中group by cube,rollup,grouping示例 开窗函数进阶last_value特别地方 深入理解 Calculate 函数 android手机文件夹目录结构 手机文件管理目录 线程间操作无效: 从不是创建控件“******”的线程访问它。 Android 整理常用的第三方库 androids上报表开发 为什么要用模块化、组件化才能完成 Android 项目中类加载功能? android多模块 安卓模块是什么意思 如何使用Android访问文件系统路径 让Android Studo 不编译某个Java文件 Java中list不包含某个元素 java list所在包 转载 解决AS插件与Gradle版本之间的对应问题
如何在Android中获取图片路径
herry507 · 2023-10-19 · via 博客园 - herry507

Android中获取图片路径的方法

在Android开发中,获取图片路径是一个常见的需求。下面介绍几种途径来实现获取图片路径的方法。

途径一:从相册中获取图片路径

要从相册中获取图片的路径,我们需要调用相应的系统接口实现。具体步骤如下:

    1. 在Manifest文件中添加获取相册的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    1. 调用系统相册,获取图片路径:

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);

    1. 在onActivityResult方法中返回图片路径:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_PICK_IMAGE) {
Uri uri = data.getData();
String imagePath = getPathFromUri(uri);
// do something with imagePath
}
}

根据Uri获取文件路径的方法:

private String getPathFromUri(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}

途径二:从拍照中获取图片路径

如果我们需要从拍照中获取图片,也可以调用相应的系统接口来实现。具体步骤如下:

    1. 在Manifest文件中添加获取拍照的权限和文件读写权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    1. 调用系统相机拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
}

在onActivityResult方法中返回相机拍照后的图片路径:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TAKE_PHOTO) {
Uri uri = data.getData();
String imagePath = getPathFromUri(uri);
// do something with imagePath
}
}

根据Uri获取文件路径的方法同上。

途径三:从文件目录中获取图片路径

我们还可以从自己的文件目录中获取已保存的图片路径,具体步骤如下:

    1. 保存图片到文件目录:

// 将图片转换为Bitmap
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// 获取文件存储路径
File filesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = new File(filesDir, "image.jpg");
// 保存图片到文件
OutputStream outputStream = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();

    1. 获取已保存的图片路径:

File filesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = new File(filesDir, "image.jpg");
String imagePath = imageFile.getAbsolutePath();
// do something with imagePath

 Environment.getExternalStorageDirectory().toString()就是获取sd卡的绝对路径,由于android各个版本不同,返回的路径也可能不相同,

使用BitmapFactory来构建一个Bitmap对象,这个对象是可以直接应用于imageView的
创建方法BitmapFactory.decodeFile("图片路径")或Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

 Ⅲ android 怎么获取相册路径

  1. private void startPickPhotoActivity() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.setType("image/*"); // Or 'image/ jpeg '
    startActivityForResult(intent, RESULT_PICK_PHOTO_NORMAL);
    }

// 获得图片返回的路径
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == RESULT_PICK_PHOTO_NORMAL) {
if (resultCode == RESULT_OK && data != null) {

//选中图片路径

mFileName = MainActivity.getPath(getApplicationContext(),
data.getData());
if ("".equals(mFileName)) {
return;
}
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("pathName", mFileName);
startActivity(intent);
}
}
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (UriUtils.isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}

}
// DownloadsProvider
else if (UriUtils.isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return UriUtils.getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (UriUtils.isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = MediaColumns._ID + "=?";
final String[] selectionArgs = new String[] { split[1] };
return UriUtils.getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (UriUtils.isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return UriUtils.getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}

Ⅶ 如何获得android assert 里图片的绝对路径

第一种方法:
String path = "file:///android_asset/文件名";
第二种方法:
InputStream abpath = getClass().getResourceAsStream("/assets/文件名");
若要想要转换成String类型
String path = new String(InputStreamToByte(abpath ));
private byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
}

 Ⅷ 安卓怎么根据图片路径调用系统相册查看这个图片

//使用Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
//Uri mUri = Uri.parse("file://" + picFile.getPath());Android3.0以后最好不要通过该方法,存在一些小Bug
intent.setDataAndType(Uri.fromFile(picFile), "image/*");
startActivity(intent);