android - 将SD卡图像设置为ImageView?

标签 android

我在 SD 卡 中有一张图片我想从 SD 卡 中获取图片并在 ImageView 中设置。请大家帮帮我已尝试以下代码。

我的代码:

 sendImageFromFolder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Uri uri=null;
                    ArrayList<Uri> arrayList=new ArrayList<Uri>();
                    ImageView imageView= (ImageView) findViewById(R.id.imageView);

                    File pictues= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    String[] listOfPictures=pictues.list();
                    for(String s:listOfPictures){
                        uri=Uri.parse("file://"+ pictues.toString() +"/"+s);
                        arrayList.add(uri);


                    }

                    InputStream inputStream = null;
                    FileOutputStream fileOutputStream=null;
                    Bitmap bmp;

                    try {
                        inputStream = getContentResolver().openInputStream(arrayList.get(0));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                    bmp = BitmapFactory.decodeStream(bufferedInputStream);

                    imageView.setImageBitmap(bmp);
                }

最佳答案

 File pictureFile = new File("Path to your image");
 Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
 mImage.setImage(bitmap);

关于android - 将SD卡图像设置为ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114785/

相关文章:

Android TextView 阴影是如何工作的

android - BottomNavigationView 再次可见后未显示

Android/Cordova 与 google-play-services 构建依赖关系错误(多个库的包名称为 'com.google.android.gms' )

java - 将 "RETR"FTP 命令与 Apache Common 的 FTPClient 结合使用

java - IllegalArgumentException:列 _id 不存在

android - 状态改变时如何关闭MediaRecorder播放的声音

Android 互联网连接检查

java - Android-与其他Fragment通信时实现接口(interface)出现空指针异常

Android - 显示图像裁剪中心

java - Android 上的 java.io 方法是否会抛出 InterruptedIOException?