android - 为什么不显示来自 SDCard 的图像?

标签 android image bitmap imagebutton filepath

我是 android 开发的菜鸟,我想在 ImageButton 中显示来自 SDCard 的图像。我的 list 中有 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);
    }

最佳答案

对于一个 FileChooser Android 插件,当你将它加入你的程序时,你需要添加 Activity 。 您尚未在 list 中添加以下代码

<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>

关于android - 为什么不显示来自 SDCard 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17196888/

相关文章:

java - 根据 android studio 中的位置动态调整线性布局大小

image - 如何将 HttpPostedFileBase 转换为图像?

java - 获取 javax.net.ssl.SSLHandshakeException : Connection closed by peer in Android 5. 0.2

java - 不知道为什么 typeConvertor 不起作用

python - 来自数组的图像和使用点的图像

php - 推特分享并附有图片

java - 无法将包含所有内容的相对布局转换为图像

java - 如何从 AsyncTask 获取位图并将其设置为我的图像

java - 将图像 Uri 从图库转换为位图 Android 时出现问题

java - 在 Android 中获取设备 ID 的最佳方法