gpt4 book ai didi

android 如何获取 sd 卡中的特定图像

转载 作者:行者123 更新时间:2023-11-30 04:38:29 24 4
gpt4 key购买 nike

我想要sdcard 中存在的任何一张图像。我只知道 image 名称。但是我如何在我的程序(包括路径)中检索该特定图像。并且还可以转换成位图或存储在位图变量中我怎样才能做到这一点?谢谢

最佳答案

这里可以显示SDCard中的所有图片,点击其中一张图片,可以得到选中图片的路径:

try {
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, MediaStore.Images.Thumbnails.IMAGE_ID);


// Get the column index of the Thumbnails Image ID
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

GridView sdcardImages = (GridView) findViewById(R.id.gallary);
sdcardImages.setAdapter(new ImageAdapter(this));

// Set up a click listener
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// Get the data location of the image
String[] projection = { MediaStore.Images.Media.DATA };
cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, null);
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
// Get image filename
String imagePath = cursor.getString(columnIndex);
// Use this path to do further processing, i.e. full screen
// display
}
});
} catch (Exception e) {
e.printStackTrace();
}

这是 ImageAdapter:

/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {

private Context context;

public ImageAdapter(Context localContext) {
context = localContext;
}

public int getCount() {
return cursor.getCount();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}

如果您知道确切的路径和图像名称,您可以使用以下方法获取 Bitmap:

Bitmap bitmap = BitmapFactory.decodeFile("yourImageDirectory" + "yourImageName.png");

关于android 如何获取 sd 卡中的特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6424257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com