2014-01-15 95 views
6

我有一個應用程序,其中有一個按鈕,從圖庫中的照片,並能正常工作,並選擇圖像後,我的應用程序節目又重新回到了活動並顯示圖像中的圖像視圖。採摘從畫廊和展示照片的圖像視圖

每一個工作正常,但有時,當我選擇一些特定的圖像預覽沒有顯示。我也曾嘗試仍壓縮圖像它不工作

我的代碼如下.. onCreate()

galeryBtn=(Button)findViewById(R.id.buttonGallery); 
galeryBtn.setOnClickListener(new OnClickListener() { 

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

    } 
}); 

在onActivityResult(INT requestCode,INT resultCode爲,意圖數據)

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]); 
    String picturePath = cursor.getString(columnIndex); 
    cursor.close(); 
    // String picturePath contains the path of selected Image 

    // Show the Selected Image on ImageView 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

} 

回答

5

我遇到這樣從資源獲取光標URI,開流,設置類似的問題位圖等,而且它一直都有錯誤。

所以我搜索庫,發現圖像選擇器,庫庫。

我敢打賭,你想從image-chooser-library

這是非常容易使用,解決了所有這些細節問題的問題對你來說,就像從Picasa等圖像

希望這是有益的嘗試這一項目您。

+0

謝謝妳,還是工作我已經在不同的手機 – Biplab

+0

測試我已經測試了,但是當我從電話給它toest選擇一些圖片「文件未找到」 – Biplab

+0

你選擇高亮圖像?是這樣的,檢查出dev_get_content分支,看看它是否工作。 –

3

您試圖在onActivityResult()中加載位圖的方式並不完全正確。有時你將無法打開圖像,並且應用程序可能會崩潰。你最好使用這樣的代碼:

Uri imageUri = data.getData(); 
InputStream imageStream = null; 
try { 
    imageStream = getContentResolver().openInputStream(imageUri); 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream)); 
} catch (FileNotFoundException e) { 
    // Handle the error 
} finally { 
    if (imageStream != null) { 
     try { 
      imageStream.close(); 
     } catch (IOException e) { 
      // Ignore the exception 
     } 
    } 
} 
5

嘗試這樣

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     switch (requestCode) { 
       case RESULT_LOAD_IMAGE: 
      if (resultCode == Activity.RESULT_OK) { 

       Uri selectedImage = intent.getData(); 
       try { 
        Bitmap bitmapImage =decodeBitmap(selectedImage); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
           // Show the Selected Image on ImageView 
        ImageView imageView = (ImageView) findViewById(R.id.imgView); 
        imageView.setImageBitmap(bitmapImage); 

      } 

而且

public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 100; 

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

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); 
    } 
+0

您的代碼工作正常。但如何增加ImageView的高度和高度? – reegan29

+1

作品般的魅力:) –

-1
final int SELECT_PICTURE = 1; 
public void openGallary(){ 
Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),SELECT_PICTURE); 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case SELECT_PICTURE: 
    Uri selectedImageUri = data.getData(); 
    imgPerview.setImageURI(selectedImageUri);}} 
0

將此圖片添加到imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

imageView.setImageURI(selectedImage); 

它適合我。