2

我是 android 开发的菜鸟,我正在尝试在 ImageButton 中显示来自 SDCard 的图像。我的清单中有 READ_EXTERNAL_STORAGE 权限,我正在使用aFileChooser库来查找我的 SD 卡上的文件。当我单击我的图像按钮时,它允许我选择我想要的图像,但不会在图像按钮中显示图像。相反,ImageButton 完全消失了。我没有收到错误,但 ImageButton 变为空白。任何帮助是极大的赞赏。

private void showChooser() {
        // Use the GET_CONTENT intent from the utility class
        Intent target = FileUtils.createGetContentIntent();
        // Create the chooser Intent
        Intent intent = Intent.createChooser(
                target, getString(R.string.chooser_title));
        try {
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            // The reason for the existence of aFileChooser
        }               
    }

    @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(RegisterActivity.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                        Log.e("File Path", file.getAbsolutePath());// Returns /external/images/media/1830
                        Log.e("BMP NULL", bmp.toString()); //Throws NullPointerException
                        Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());//Seems to work fine here
                        userpic.setImageBitmap(bmp);  //ImageButton disappears/goes blank here.
                    } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error", e);
                    }
                }
            } 
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
4

2 回答 2

1

对于 aFileChooser Android 插件,当您将其加入您的程序时,您需要添加活动。您尚未在清单中添加以下代码

<activity
            android:name="com.ipaulpro.afilechooser.FileChooserActivity"
            android:exported="false"
            android:icon="@drawable/ic_chooser"
            android:label="@string/choose_file"
            android:theme="@style/ChooserTheme" >
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />

                <data android:mimeType="*/*" />
            </intent-filter>
</activity>
于 2013-11-22T08:09:38.070 回答
0

我可以在没有 fileChooser 的情况下为您提供解决方案...希望对您有所帮助

仅检查数据是否!= NULL,并发送请求代码!

public final int SELECT_PICTURE = 324234; //some value

第一的 ...

        ContentValues values = new ContentValues();
        String title = "blabla";
        String descritpion = "blabla";
        values.put(MediaStore.Images.Media.TITLE, title);
        values.put(MediaStore.Images.Media.DESCRIPTION, descritpion);
        imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(takePhotoIntent, SELECT_PICTURE);

...

然后

if (requestCode == SELECT_PICTURE && data != null)
    try {
                    // !!! could be every format!!!
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    if (cursor == null) {
                        Toast.makeText(getApplicationContext(),"imageunguilty", Toast.LENGTH_SHORT).show();
                        break;
                    }
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    File src = new File(picturePath);
                    Bitmap b = decodeFile(src.getAbsolutePath());
                    //here recommended to resize your bitmap due to OutOfMemoryError!!!
                    if (b != null) {
                        // UpdatePicture

                    } else {
                        Toast.makeText(getApplicationContext(), "no image selected?", Toast.LENGTH_SHORT).show();
                    }
                } catch (OutOfMemoryError e) {
                    Log.e("Pictures", "" + e.getMessage());
                }

在您的代码 Log.e("BMP NULL", bmp.toString()); //抛出NullPointerException,因为之前没有定义bmp!

于 2013-08-04T08:05:35.827 回答