Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将私人文件中的图片加载到版面(Android)?_Android_File_Layout - Fatal编程技术网

如何将私人文件中的图片加载到版面(Android)?

如何将私人文件中的图片加载到版面(Android)?,android,file,layout,Android,File,Layout,我有一张图片保存在/data/data/my.package.name/files/mypicture.png中,我想将其加载到布局中 我试过这个,但不起作用 File filePath = getFileStreamPath("pictureIWouldLikeToLoad.png"); ImageView img = new ImageView(getApplicationContext()); img.setImageDrawable(Drawable.createFromPath(fil

我有一张图片保存在
/data/data/my.package.name/files/mypicture.png
中,我想将其加载到布局中

我试过这个,但不起作用

File filePath = getFileStreamPath("pictureIWouldLikeToLoad.png");
ImageView img = new ImageView(getApplicationContext());
img.setImageDrawable(Drawable.createFromPath(filePath.toString()));
ViewGroup picturesLayout = (ViewGroup) findViewById(R.id.layout_pictures_area);
picturesLayout.addView(imgView);

我不知道它是怎么不起作用的,有人能解释一下为什么吗?那我该怎么做呢?我需要内容提供商吗?有更简单的方法吗?

您应该在XML中为要更改的图像定义一个ID

<ImageView
android:id="@+id/awesome_image"
.
.
.
/>
最后获得imageView对象并设置其位图

ImageView imageView = (ImageView) findViewById(R.id.awesome_image);
imageView.setImageBitmap(bmp);

所以我成功了。也许可以改进,但这里有一些实际可行的方法:

FileInputStream inputStream = openFileInput("my_image.png");
BufferedInputStream bufferedInput = new BufferedInputStream(inputStream);                       
Bitmap bmp = BitmapFactory.decodeStream(bufferedInput);
ImageView imgView = (ImageView) findViewById(R.id.my_imageview);
imgView.setImageBitmap(bmp);
bufferedInput.close();
inputStream.close();

顺便说一下,您可以像这样直接给
decodeFile
方法提供路径:
Bitmap bmp=BitmapFactory.decodeFile(“/data/data/my.package.name/files/mypicture.png”)
FileInputStream inputStream = openFileInput("my_image.png");
BufferedInputStream bufferedInput = new BufferedInputStream(inputStream);                       
Bitmap bmp = BitmapFactory.decodeStream(bufferedInput);
ImageView imgView = (ImageView) findViewById(R.id.my_imageview);
imgView.setImageBitmap(bmp);
bufferedInput.close();
inputStream.close();