SORU
24 Mart 2010, ÇARŞAMBA


Nasıl benim app için Galeri içinden bir görüntü (SD Kart) seçmek için?

Bu soru aslında Android 1.6 için istendi.

Benim app fotoğrafları seçenekleri üzerinde çalışıyorum.

Bir düğme ve benim aktivitede bir İmageView var. Ben düğmesine tıkladığınızda Galerisi için yönlendirme gideceğini ve bir görüntü seçmek mümkün olacak. Seçili resim benim İmageView görünmesini istiyorsunuz.

CEVAP
23 ŞUBAT 2011, ÇARŞAMBA


private static final int SELECT_PHOTO = 100;

Niyet başlayın

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);    

İşlem sonucu

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream = getContentResolver().openInputStream(selectedImage);
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
        }
    }
}

Alternatif olarak, görüntü OutOfMemory hataları önlemek için altörnekleyebilirsiniz.

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 140;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
               || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

    }

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • HereWeStayKings

    HereWeStayKi

    3 NİSAN 2013
  • bored before i even began

    bored before

    30 Mart 2009
  • WPBeginner - WordPress Tutorials

    WPBeginner -

    17 Temmuz 2009