2013-08-14 13 views
7

Hi Zajmuję się tworzeniem aplikacji Android galerii, gdzie ja ściągam zdjęć z wbudowanego w galerii i wyświetlania it.I używam kod jak poniżejpobierania obrazów z galerii na Androidzie telefonów z pamięci wewnętrznej

 String[] projection = {MediaStore.Images.Thumbnails._ID}; 


    Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI, 
       projection, // Which columns to return 
       null,  // Return all rows 
       null,  
       null); 


     int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
     int size = cursor.getCount(); 
     // If size is 0, there are no images on the SD Card. 
     if (size == 0) 
     { 
      Log.e("size 0","0"); 
     } 

problemu Kiedy uruchamiam ten kod na telefonach posiadających tylko pamięć wewnętrzną (Galaxy Nexus), otrzymuję dziennik, który mówi, że rozmiar jest zerowy, mimo że w galerii wbudowanej znajdują się obrazy. Jak rozwiązać ten problem. Please Help.Thanks!

+0

Spróbuj zmienić "MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI" na "MediaStore.Images.Media.EXTERNAL_CONTENT_URI" – Tarun

+0

Dostęp do multimediów użytkownika ma EXTERNAL_CONTENT_URI. INTERNAL_CONTENT_URI jest używane tylko przez aplikacje systemowe. – Tarun

+0

Próbowałem EXTERNAL_CONTENT_URI, a także INTERNAL_CONTENT_URI, EXTERNAL działa dobrze na telefonach z kartą SD, ale nie działa na telefonach takich jak Galaxy Nexus (bez zewnętrznego magazynu). – sanjana

Odpowiedz

2

Aby uzyskać listę galerii zdjęć możesz spróbować tego

String[] projection = new String[]{ 
     MediaStore.Images.Media._ID, 
     MediaStore.Images.Media.BUCKET_DISPLAY_NAME, 
     MediaStore.Images.Media.DATE_TAKEN 
}; 


Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 


Cursor cur = getContentResolver().query(imageUri, 
     projection, // Which columns to return 
     null,  // Which rows to return (all rows) 
     null,  // Selection arguments (none) 
     null  // Ordering 
     ); 

Log.i("Images Count"," count="+cur.getCount()); 
+0

managedQuery jest przestarzałe. zamiast tego należy użyć funkcji getContentResolver(). query (...). –

2

Spróbuj na przycisk jak „Przeglądaj”

browse.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 

     Intent i = new Intent(
       Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 

    } 
}); 

i cal także ustawić wybrany obraz do ImageView jak

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      imageView = (ImageView) findViewById(R.id.property_image); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     } 

} 

W pierwszym bloku kodu użyć startActivityForResult(i, RESULT_LOAD_IMAGE); ten wynik powrotu do zwany a aktywność d możemy uzyskać ten wynik przez drugiego bloku kodu i ustawić wybrany obraz w ImageView

2
String[] projection = {MediaStore.Images.Media._ID}; 

Wymień MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI z MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI

Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       projection, // Which columns to return 
       null,  // Return all rows 
       null,  
       null); 


    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); 
    int size = cursor.getCount(); 
    // If size is 0, there are no images on the SD Card. 
    if (size == 0) 
    { 
     Log.e("size 0","0"); 
    } 

EDIT: Get listy miniatur i uzyskać identyfikator URI obrazu z kursora:


Uri uri=MediaStore.Images.Thumbnails.getContentUri("external"); 

Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails 
     (getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null); 

if(cursor != null && cursor.getCount() > 0) { 
    String uri = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA)); 
} 
Powiązane problemy