2

我已经检索了保存在模拟器 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);

    }


 }
}

任何帮助将不胜感激!

4

1 回答 1

1

将代码更改为从 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);

    }


 }
于 2012-12-07T22:46:05.293 回答