为什么getBitmap方法不起作用?

时间:2013-07-19 16:57:49

标签: android image bitmap uri media

我是Android的菜鸟,我想设置一个带有SD卡文件的ImageButton图像。但是,getBitmap没有创建工作位图。当我使用刚刚创建的位图设置ImageButton时,imageButton的尺寸会发生变化但图像不会出现。这真的令人沮丧,非常感谢任何帮助解决这个问题。

mycode的

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_CODE:  
            // If the file selection was successful
            if (resultCode == RESULT_OK) {      
                if (data != null) {
                    // Get the URI of the selected file
                    final Uri uri = data.getData();

                    try {
                        // Create a file instance from the URI
                        final File file = FileUtils.getFile(uri);

                        Toast.makeText(Profile_Barber.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                        Log.e("URI", uri.toString());//Returns:  content://media/external/images/media/1834

                        Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

                        if(bmp==null){
                            Log.e("BMP NULL", "This that bullshit!");
                        }else{
                            Log.e("BMP NOT NULL", bmp.toString()); //Returns: BMP NOT NULL android.graphics.Bitmap@4152b5a0

                            profilepic.setImageBitmap(bmp);
                        }

 } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error", e);
                        e.printStackTrace();
                    }
                }
            } 
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

1 个答案:

答案 0 :(得分:1)

如何使用它来解码图像?

        Uri contentURI = Uri.parse(data.getDataString());        
        ContentResolver cr = getContentResolver();
        InputStream in = cr.openInputStream(contentURI);
        Bitmap pic = BitmapFactory.decodeStream(in,null,null);