为什么不从SDCard显示图像?

时间:2013-06-19 16:47:51

标签: android image bitmap imagebutton filepath

我是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);
    }

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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!