2010-11-16 35 views
39

Tôi đang tìm kiếm: Danh sách tên thư viện ảnh hiện tại (hy vọng hình thu nhỏ hàng đầu của chúng) Nội dung của thư viện (sau đó tôi có thể tải hình thu nhỏ và kích thước đầy đủ nếu cần)Lấy danh sách các thư viện ảnh trên Android

Làm cách nào để tìm danh sách "Phòng trưng bày" (không biết đó có phải là cụm từ thích hợp trong android cho các nhóm hình ảnh hiển thị trong ứng dụng Thư viện ...) và nội dung của chúng không? Tôi cần truy cập vào thư viện trong cấu trúc của nó mà không sử dụng màn hình bộ sưu tập hiện có (tôi đang tạo một bộ sưu tập hoàn toàn mới, không phải là một lớp trên cho người yêu cầu ảnh, v.v.)

Tôi giả định MediaStore.Images là nơi tôi cần nhưng tôi không thấy bất cứ điều gì sẽ cho tôi các nhóm ...

Trả lời

73

Các nhóm được xác định bởi MediaStore.Images.Media.BUCKET_DISPLAY_NAME. Dưới đây là đoạn code mẫu để liệt kê những hình ảnh và đăng nhập tên xô và date_taken của họ:

// which image properties are we querying 
String[] projection = new String[] { 
     MediaStore.Images.Media._ID, 
     MediaStore.Images.Media.BUCKET_DISPLAY_NAME, 
     MediaStore.Images.Media.DATE_TAKEN 
}; 

// content:// style URI for the "primary" external storage volume 
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

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

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

if (cur.moveToFirst()) { 
    String bucket; 
    String date; 
    int bucketColumn = cur.getColumnIndex(
     MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 

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

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

     // Do something with the values. 
     Log.i("ListingImages", " bucket=" + bucket 
       + " date_taken=" + date); 
    } while (cur.moveToNext()); 

} 
+0

AH! nó hoạt động giống như truy vấn cơ sở dữ liệu thô. mã ví dụ hoàn hảo cảm ơn bạn! – ima747

+0

Tôi có thể hiển thị ảnh như thế nào ??? – Marya

+0

cách lấy đường dẫn hình ảnh? cho mỗi ngàyToken? –

33
/** 
* Getting All Images Path 
* 
* @param activity 
* @return ArrayList with images Path 
*/ 
public static ArrayList<String> getAllShownImagesPath(Activity activity) { 
    Uri uri; 
    Cursor cursor; 
    int column_index_data, column_index_folder_name; 
    ArrayList<String> listOfAllImages = new ArrayList<String>(); 
    String absolutePathOfImage = null; 
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

    String[] projection = { MediaColumns.DATA, 
      MediaStore.Images.Media.BUCKET_DISPLAY_NAME }; 

    cursor = activity.getContentResolver().query(uri, projection, null, 
      null, null); 

    column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
    column_index_folder_name = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 
    while (cursor.moveToNext()) { 
     absolutePathOfImage = cursor.getString(column_index_data); 

     listOfAllImages.add(absolutePathOfImage); 
    } 
    return listOfAllImages; 
} 
+13

Đừng quên thêm vào tệp kê khai của bạn. –

15

đây là giải pháp đầy đủ trong vài bước đơn giản:

Vài bước tiếp theo sẽ guid bạn làm thế nào để tạo ra một Vector mà sẽ giữ các album được tìm thấy trên một thiết bị nhất định. Mỗi Album sẽ giữ một hình ảnh xem trước cũng như một bên trong Vector sẽ giữ tất cả các hình ảnh của Album.

  1. Tạo đối tượng sẽ giữ hình ảnh sau khi được trích xuất khỏi bộ nhớ. Chúng ta sẽ gọi nó là PhoneAlbum. Đây là cách nó sẽ trông:

    public class PhoneAlbum { 
    
        private int id; 
        private String name; 
        private String coverUri; 
        private Vector<PhonePhoto> albumPhotos; 
    
        public int getId() { 
         return id; 
        } 
    
        public void setId(int id) { 
         this.id = id; 
        } 
    
        public String getName() { 
         return name; 
        } 
    
        public void setName(String name) { 
         this.name = name; 
        } 
    
        public String getCoverUri() { 
         return coverUri; 
        } 
    
        public void setCoverUri(String albumCoverUri) { 
         this.coverUri = albumCoverUri; 
        } 
    
        public Vector<PhonePhoto> getAlbumPhotos() { 
         if (albumPhotos == null) { 
          albumPhotos = new Vector<>(); 
         } 
         return albumPhotos; 
        } 
    
        public void setAlbumPhotos(Vector<PhonePhoto> albumPhotos) { 
         this.albumPhotos = albumPhotos; 
        } 
    } 
    
  2. Tạo một đối tượng mà sẽ giữ hình ảnh trong album mang tên: PhonePhoto

    public class PhonePhoto { 
    
        private int id; 
        private String albumName; 
        private String photoUri; 
    
        public int getId() { 
         return id; 
        } 
    
        public void setId(int id) { 
         this.id = id; 
        } 
    
        public String getAlbumName() { 
         return albumName; 
        } 
    
        public void setAlbumName(String name) { 
         this.albumName = name; 
        } 
    
        public String getPhotoUri() { 
         return photoUri; 
        } 
    
        public void setPhotoUri(String photoUri) { 
         this.photoUri = photoUri; 
        } 
    } 
    
  3. Tạo một giao diện để xử lý những hình ảnh trích ra sau khi hoàn thành. Chúng ta sẽ gọi nó là OnPhoneImagesObtained. Ở đây là:

    public interface OnPhoneImagesObtained { 
    
        void onComplete(Vector<PhoneAlbum> albums); 
        void onError(); 
    
    } 
    
  4. Tạo một lớp mới: DeviceImageManager

    public class DeviceImageManager { 
    
    } 
    
  5. Khi bạn đã tạo DeviceImageManager, thêm phương pháp sau:

    public static void getPhoneAlbums(Context context , OnPhoneImagesObtained listener){ 
        // Creating vectors to hold the final albums objects and albums names 
        Vector<PhoneAlbum> phoneAlbums = new Vector<>(); 
        Vector<String> albumsNames = new Vector<>(); 
    
        // which image properties are we querying 
        String[] projection = new String[] { 
          MediaStore.Images.Media.BUCKET_DISPLAY_NAME, 
          MediaStore.Images.Media.DATA, 
          MediaStore.Images.Media._ID 
        }; 
    
        // content: style URI for the "primary" external storage volume 
        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    
        // Make the query. 
        Cursor cur = context.getContentResolver().query(images, 
          projection, // Which columns to return 
          null,  // Which rows to return (all rows) 
          null,  // Selection arguments (none) 
          null  // Ordering 
        ); 
    
        if (cur != null && cur.getCount() > 0) { 
         Log.i("DeviceImageManager"," query count=" + cur.getCount()); 
    
         if (cur.moveToFirst()) { 
          String bucketName; 
          String data; 
          String imageId; 
          int bucketNameColumn = cur.getColumnIndex(
           MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 
    
          int imageUriColumn = cur.getColumnIndex(
           MediaStore.Images.Media.DATA); 
    
          int imageIdColumn = cur.getColumnIndex(
           MediaStore.Images.Media._ID); 
    
          do { 
           // Get the field values 
           bucketName = cur.getString(bucketNameColumn); 
           data = cur.getString(imageUriColumn); 
           imageId = cur.getString(imageIdColumn); 
    
           // Adding a new PhonePhoto object to phonePhotos vector 
           PhonePhoto phonePhoto = new PhonePhoto(); 
           phonePhoto.setAlbumName(bucketName); 
           phonePhoto.setPhotoUri(data); 
           phonePhoto.setId(Integer.valueOf(imageId)); 
    
           if (albumsNames.contains(bucketName)) { 
            for (PhoneAlbum album : phoneAlbums) { 
             if (album.getName().equals(bucketName)) { 
              album.getAlbumPhotos().add(phonePhoto); 
              Log.i("DeviceImageManager", "A photo was added to album => " + bucketName); 
              break; 
             } 
            } 
           } else { 
            PhoneAlbum album = new PhoneAlbum(); 
            Log.i("DeviceImageManager", "A new album was created => " + bucketName); 
            album.setId(phonePhoto.getId()); 
            album.setName(bucketName); 
            album.setCoverUri(phonePhoto.getPhotoUri()); 
            album.getAlbumPhotos().add(phonePhoto); 
            Log.i("DeviceImageManager", "A photo was added to album => " + bucketName); 
    
            phoneAlbums.add(album); 
            albumsNames.add(bucketName); 
           } 
    
          } while (cur.moveToNext()); 
         } 
    
         cur.close(); 
         listener.onComplete(phoneAlbums); 
        } else { 
         listener.onError(); 
        } 
    } 
    
  6. Bây giờ tất cả các bạn đã để lại là để hiển thị hình ảnh thành màn hình. Trong trường hợp của tôi, tôi thích sử dụng Picasso. Dưới đây là cách tôi làm điều đó:

    Picasso.with(getActivity()) 
           .load("file:" + mPhoneAlbums.get(i).getCoverUri()) 
           .centerCrop() 
           .fit() 
           .placeholder(R.drawable.temp_image) 
           .into(mAlbumPreview); 
    
  7. Đừng quên thêm một phép đọc lưu trữ bên ngoài trong biểu hiện của bạn:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
    

Vậy là xong. Bạn rất tốt để đi! Chúc may mắn!

1

Tải về mã nguồn từ đây (Get all images from gallery in android programmatically)

activity_main.xml

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 


    <GridView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/gv_folder" 
     android:numColumns="2" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp"></GridView> 


</RelativeLayout> 

MainActivity.java

package galleryimages.galleryimages; 

import android.Manifest; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.MediaStore; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.GridView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    public static ArrayList<Model_images> al_images = new ArrayList<>(); 
    boolean boolean_folder; 
    Adapter_PhotosFolder obj_adapter; 
    GridView gv_folder; 
    private static final int REQUEST_PERMISSIONS = 100; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     gv_folder = (GridView)findViewById(R.id.gv_folder); 

     gv_folder.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Intent intent = new Intent(getApplicationContext(), PhotosActivity.class); 
       intent.putExtra("value",i); 
       startActivity(intent); 
      } 
     }); 


     if ((ContextCompat.checkSelfPermission(getApplicationContext(), 
       Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(), 
       Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { 
      if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
        Manifest.permission.WRITE_EXTERNAL_STORAGE)) && (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
        Manifest.permission.READ_EXTERNAL_STORAGE))) { 

      } else { 
       ActivityCompat.requestPermissions(MainActivity.this, 
         new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 
         REQUEST_PERMISSIONS); 
      } 
     }else { 
      Log.e("Else","Else"); 
      fn_imagespath(); 
     } 



    } 

    public ArrayList<Model_images> fn_imagespath() { 
     al_images.clear(); 

     int int_position = 0; 
     Uri uri; 
     Cursor cursor; 
     int column_index_data, column_index_folder_name; 

     String absolutePathOfImage = null; 
     uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

     String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME}; 

     final String orderBy = MediaStore.Images.Media.DATE_TAKEN; 
     cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC"); 

     column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
     column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); 
     while (cursor.moveToNext()) { 
      absolutePathOfImage = cursor.getString(column_index_data); 
      Log.e("Column", absolutePathOfImage); 
      Log.e("Folder", cursor.getString(column_index_folder_name)); 

      for (int i = 0; i < al_images.size(); i++) { 
       if (al_images.get(i).getStr_folder().equals(cursor.getString(column_index_folder_name))) { 
        boolean_folder = true; 
        int_position = i; 
        break; 
       } else { 
        boolean_folder = false; 
       } 
      } 


      if (boolean_folder) { 

       ArrayList<String> al_path = new ArrayList<>(); 
       al_path.addAll(al_images.get(int_position).getAl_imagepath()); 
       al_path.add(absolutePathOfImage); 
       al_images.get(int_position).setAl_imagepath(al_path); 

      } else { 
       ArrayList<String> al_path = new ArrayList<>(); 
       al_path.add(absolutePathOfImage); 
       Model_images obj_model = new Model_images(); 
       obj_model.setStr_folder(cursor.getString(column_index_folder_name)); 
       obj_model.setAl_imagepath(al_path); 

       al_images.add(obj_model); 


      } 


     } 


     for (int i = 0; i < al_images.size(); i++) { 
      Log.e("FOLDER", al_images.get(i).getStr_folder()); 
      for (int j = 0; j < al_images.get(i).getAl_imagepath().size(); j++) { 
       Log.e("FILE", al_images.get(i).getAl_imagepath().get(j)); 
      } 
     } 
     obj_adapter = new Adapter_PhotosFolder(getApplicationContext(),al_images); 
     gv_folder.setAdapter(obj_adapter); 
     return al_images; 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     switch (requestCode) { 
      case REQUEST_PERMISSIONS: { 
       for (int i = 0; i < grantResults.length; i++) { 
        if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         fn_imagespath(); 
        } else { 
         Toast.makeText(MainActivity.this, "The app was not allowed to read or write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show(); 
        } 
       } 
      } 
     } 
    } 

} 
Các vấn đề liên quan