2015-01-26 206 views
1
選擇

所以我有一個加載庫,並使用此代碼選擇圖像的按鈕...ImageView的不顯示圖像從畫廊

public void getGalleryImage(View v){ 
    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    startActivityForResult(intent, 1); 
} 

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]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 

     galleryImage.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 


} 

但是,當它返回到原來的活動中,ImageView的還是沒有顯示任何東西。它不會給我任何錯誤,或者類似的東西。這裏的ImageView的XML ...

<ImageView 
    android:id="@+id/galleryImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

我怎樣才能讓它顯示?

+0

我認爲[此鏈接] [1]會幫助你。 [1]:http://stackoverflow.com/questions/10860562/how-to-display-selected-gallery-image-in-imageview-in-android – 2015-01-26 18:06:59

+0

檢查此主題http://stackoverflow.com/問題/ 3879992/get-bitmap-from-an-uri-android 所有的最好的 – AndroidRuntimeException 2015-01-26 18:09:14

回答

2

我最終找出了一些東西。我只是做...

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     galleryImage.setImageURI(selectedImage); 

    } 

這工作爲我想要完成的。

+0

它的工作就像魅力...感謝兄弟 – Hoo 2015-10-22 09:28:49

1

試試這個:

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(); 
     galleryImage.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri)); 
    } 
} 
+0

這也適用。我結束了改變我的代碼到這樣的事情,除了我試着繞過它。 – ThatBoiJo 2015-01-26 20:26:49

0
public class MainActivity extends AppCompatActivity{ 

    ImageView image; 
    Button pick; 

    int TAG_IMAGE = 100; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     image = (ImageView) findViewById(R.id.imageView); 

     pick = (Button) findViewById(R.id.button); 

     pick.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       intent.setType("*/*"); 


       startActivityForResult(Intent.createChooser(intent,"Select Image "),TAG_IMAGE); 

      } 
     }); 


    } 

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

     if(requestCode == TAG_IMAGE && resultCode == RESULT_OK) 
     { 
      Uri selectedImage = data.getData(); 


      try 
      { 

       String[] filePath = {MediaStore.Images.Media.DATA}; 

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

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

       String images = cursor.getString(columnIndex); 



       image.setImageURI(selectedImage); 


      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 


    } 



} 
+0

此代碼適用於我。 – 2017-02-23 11:40:44