简体   繁体   English

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

[英]Android: Read any specific image from sd card and display it as bitmap

I have retrieved the name of the very first image saved in the media on the sdcard of the emulator. 我已经检索了保存在模拟器sdcard上媒体中的第一张图像的名称。 Now I want to display the image corresponding to it.. How can I do this ? 现在,我要显示与其对应的图像。我该怎么做?

Here's the code 这是代码

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);

    }


 }
}

Any help will be highly appreciated ! 任何帮助将不胜感激!

Change your code as for getting image path from MediaStore and then set it to ImageView: 更改代码以从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);

    }


 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM