1

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

我的密码

@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);
    }
4

1 回答 1

1

用它来解码图像怎么样?

        Uri contentURI = Uri.parse(data.getDataString());        
        ContentResolver cr = getContentResolver();
        InputStream in = cr.openInputStream(contentURI);
        Bitmap pic = BitmapFactory.decodeStream(in,null,null);
于 2013-07-19T17:19:01.613 回答