gpt4 book ai didi

Android:从SD卡读取任何特定图像并将其显示为位图

转载 作者:行者123 更新时间:2023-11-29 01:58:48 27 4
gpt4 key购买 nike

我已经检索到保存在模拟器 SD 卡上的媒体中的第一个图像的名称。现在我想显示对应的图像..我该怎么做?

这是代码

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class ClientActivity extends Activity {

ImageView imageView;
TextView textView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);


imageView = (ImageView) findViewById(R.id.imageView1);
textView = (TextView) findViewById(R.id.textview1);
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.TITLE,

};

// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Log.i("URI", images.toString());

// Make the query.
Cursor cur = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Thumbnails.IMAGE_ID // Ordering
);

Log.i("ListingImages"," query count="+cur.getCount());

if (cur.moveToFirst()) {
String bucket;
String date;
String name;


int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);

int nameColumn = cur.getColumnIndex(
MediaStore.Images.Media.TITLE);


// Get the field values
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
name = cur.getString(nameColumn);



// Do something with the values.
Log.i("ListingImages", " bucket=" + bucket
+ " name_taken=" + name);

}


}
}

任何帮助将不胜感激!

最佳答案

更改代码以从 MediaStore 获取图像路径,然后将其设置为 ImageView:

//YOUR CODE HERE...
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA, // add DATA column
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.TITLE,

};

// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Log.i("URI", images.toString());

// Make the query.
Cursor cur = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Thumbnails.IMAGE_ID // Ordering
);

Log.i("ListingImages"," query count="+cur.getCount());

if (cur.moveToFirst()) {
String bucket;
String date;
String name;


int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);

int nameColumn = cur.getColumnIndex(
MediaStore.Images.Media.TITLE);


// Get the field values
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
name = cur.getString(nameColumn);
int columnIndex = cur.getColumnIndex(MediaStore.Images.Media.DATA);
String picPath = cur.getString(columnIndex);

imageView.setImageBitmap(BitmapFactory.decodeFile(picPath));
// Do something with the values.
Log.i("ListingImages", " bucket=" + bucket
+ " name_taken=" + name);

}


}

关于Android:从SD卡读取任何特定图像并将其显示为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772138/

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