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 に答える