2016-06-21 30 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

我做了,但它不会呈现位图。 –