2016-06-21 63 views

回答

0

創建一個Intent挑選的圖像從畫廊這樣

創建一個全局變量

private int OPEN_GALLERY = 101

調用下面的意圖上的按鈕或某事的點擊。

Intent intent = new Intent(
             Intent.ACTION_PICK, 
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
           intent.setType("image/*"); 
           startActivityForResult(
             Intent.createChooser(intent, "Select Picture"), 
             OPEN_GALLERY); 

獲取使用onActivityResult()

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


     case OPEN_GALLERY: 
      if (data != null) { 


       try { 
        Uri selectedImage = data.getData(); 
        String[] filePath = {MediaStore.Images.Media.DATA}; 
        Cursor c = getApplicationContext().getContentResolver().query(
          selectedImage, filePath, null, null, null); 
        c.moveToFirst(); 
        int columnIndex = c.getColumnIndex(filePath[0]); 
        String picturePath = c.getString(columnIndex); 
        c.close(); 

        BitmapFactory.Options options = new BitmapFactory.Options(); 
        Bitmap bm = BitmapFactory.decodeFile(picturePath,options); 

        //Pass bitmap to loadtextures method 
        loadTextures(bm); 


       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       break; 

      } 
    } 
} 

通行證位圖所選圖像到loadTextures()方法這樣

private void loadTextures(Bitmap bm) { 
// Generate textures 
GLES20.glGenTextures(2, mTextures, 0); 
// Load input bitmap 
Bitmap bitmap = bm; 
mImageWidth = bitmap.getWidth(); 
mImageHeight = bitmap.getHeight(); 
mTexRenderer.updateTextureSize(mImageWidth, mImageHeight); 
// Upload to texture 
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]); 
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); 
// Set texture parameters 
GLToolbox.initTexParams(); 
} 
+0

我能得到從畫廊的位圖,但在那之後沒能將位圖設置爲博客文章代碼。 –

+0

抱歉,我沒有注意到您正在使用GLSurfaceView。編輯我的答案。請檢查一下。在'onActivityResult()'方法中調用'loadTextures(bm)'。 – SripadRaj

+0

我做了,但它不會呈現位圖。 –