如何在Android中获取图片路径

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);
posted @ 2023-10-19 14:57  herry507  阅读(2038)  评论(0编辑  收藏  举报