2013-06-04 20 views
15

Szukam sposobu na otwarcie aplikacji galerii Android z zamiarem.Otwórz aplikację Galeria z Android Intent

Nie chcę zwracać obrazu, ale po prostu otwórz galerię, aby umożliwić użytkownikowi korzystanie z niej tak, jakby wybrał ją z programu uruchamiającego (View pictures/folders).

Próbowałem wykonać następujące czynności:

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_GET_CONTENT); 
intent.setType("image/*"); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

Jednak to powoduje aplikację aby zamknąć raz wybrać zdjęcie (wiem, że to ze względu na ACTION_GET_CONTENT), ale muszę po prostu otworzyć Galeria.

Każda pomoc będzie świetna.

Dzięki

+0

ten powinien odpowiedzieć na to pytanie ... http://stackoverflow.com/a/4979492/1620738 – UpLate

+0

Jaka jest twoja wersja minSdkVersion? – ozbek

+0

MinSdkVersion to 8 – Dfranc3373

Odpowiedz

7

Można otworzyć galerię stosując następujące intencje:

public static final int RESULT_GALLERY = 0; 

Intent galleryIntent = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(galleryIntent , RESULT_GALLERY); 

Jeśli chcesz uzyskać wynik URI czy cokolwiek innego wykorzystania tego:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case QuestionEntryView.RESULT_GALLERY : 
     if (null != data) { 
      imageUri = data.getData(); 
      //Do whatever that you desire here. or leave this blank 

     } 
     break; 
    default: 
     break; 
    } 
} 

Testowane na Androida 4.0+

+0

Bardzo dziękuję za to :) –

3

Ponieważ nie chcesz, aby wynik został zwrócony, spróbuj wykonać prosty kod.

Intent i=new Intent(Intent.ACTION_PICK); 
      i.setType("image/*"); 
      startActivity(i); 
28

Mam nadzieję, że to ci pomoże. Ten kod działa dla mnie.

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

ResultCode służy do aktualizowania wybrany obraz do galerii Zobacz

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

     if (cursor == null || cursor.getCount() < 1) { 
      return; // no cursor or no record. DO YOUR ERROR HANDLING 
     } 

     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 

     if(columnIndex < 0) // no column index 
      return; // DO YOUR ERROR HANDLING 

     String picturePath = cursor.getString(columnIndex); 

     cursor.close(); // close cursor 

     photo = decodeFilePath(picturePath.toString()); 

     List<Bitmap> bitmap = new ArrayList<Bitmap>(); 
     bitmap.add(photo); 
     ImageAdapter imageAdapter = new ImageAdapter(
       AddIncidentScreen.this, bitmap); 
     imageAdapter.notifyDataSetChanged(); 
     newTagImage.setAdapter(imageAdapter); 
    } 
+1

Czy kursor nie powinien być zamknięty? –

+0

picturePath ma zerową wartość w moim przypadku (Android 7.0). – Makalele

+1

Nie można zaimportować decodeFilePath –

10

To jest to, czego potrzebujesz:

ACTION_VIEW 

Zmień swój kod do:

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
intent.setType("image/*"); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 
+1

Jest to jedyna odpowiedź, która odpowiada na pytanie! On po prostu musi * muszę otworzyć galerię. * Nie wybrać ani nie zwrócić nada – Enoobong

4
  1. Następujące można wykorzystać w Activity lub Fragment.

    private File mCurrentPhoto; 
    
  2. dodać uprawnienia

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> 
    
  3. Dodaj planująca otworzyć "obraz selektor" i "foto-capture"

    //A system-based view to select photos. 
    private void dispatchPhotoSelectionIntent() { 
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR); 
    } 
    
    
    
        //Open system camera application to capture a photo. 
        private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) { 
        try { 
         createImageFile(); 
        } catch (IOException ex) { 
         // Error occurred while creating the File 
        } 
        // Continue only if the File was successfully created 
        if (mCurrentPhoto != null) { 
         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto)); 
         startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
        } 
    } 
    } 
    
  4. Dodaj manipulację przy wsiadaniu zdjęcie.

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    case REQUEST_IMAGE_SELECTOR: 
        if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { 
         String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
         Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null); 
         if (cursor == null || cursor.getCount() < 1) { 
          mCurrentPhoto = null; 
          break; 
         } 
         cursor.moveToFirst(); 
         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         if(columnIndex < 0) { // no column index 
          mCurrentPhoto = null; 
          break; 
         } 
         mCurrentPhoto = new File(cursor.getString(columnIndex)); 
         cursor.close(); 
        } else { 
         mCurrentPhoto = null; 
        } 
        break; 
    case REQUEST_IMAGE_CAPTURE: 
        if (resultCode != Activity.RESULT_OK) { 
         mCurrentPhoto = null; 
        } 
        break; 
    } 
    if (mCurrentPhoto != null) { 
        ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv); 
        Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
    } 
    
+0

Czy uprawnienie jest naprawdę wymagane, aby otworzyć galerię. Nie musiałem korzystać z żadnych uprawnień, aby otworzyć galerię. –

+0

@IsharaAmarasekera, jeśli się nie zawiesza i wszystko jest w porządku, to nie – TeeTracker

Powiązane problemy