gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:52:16 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com