如何在Android应用程序中显示图像

时间:2022-09-04 08:19:23

I want to display Image in my Android application in specific size. How can I do it? Please guide me? And one more thing I want that image from SD card. So please Help me.

我想在我的Android应用程序中以特定大小显示Image。我该怎么做?请指导我?还有一件事我想从SD卡那里获得这张图片。所以请帮助我。

Thanks in advance.

提前致谢。

2 个解决方案

#1


8  

First you need to create an imageview.

首先,您需要创建一个imageview。

ImageView imageView = new ImageView(getApplicationContext());

Create layout param to add imageview on layout

创建布局参数以在布局上添加imageview

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Then get your image path

然后获取您的图像路径

String path = Environment.getExternalStorageDirectory() + "/your folder name/image_name.bmp";

Set your image on ImageView

在ImageView上设置图像

Bitmap image = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(image);

Fetch your layout on which you want to add

获取要添加的布局

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout1);

Add your view to the layout

将视图添加到布局中

rl.addView(imageView, lp);

#2


2  

You can use the below code snippet to load image in imageview .

您可以使用以下代码段在imageview中加载图片。

ImageView iv = new ImageView(this);
iv.setImageUri(Uri.parse("file://loaction"));

you can use the above sample method to display image in image from sd card. Even dont hard code the file path use Environment.getExternalEnvironment() to get path.

您可以使用上面的示例方法从SD卡中显示图像中的图像。即使不对文件路径进行硬编码也要使用Environment.getExternalEnvironment()来获取路径。

Environment.getExternalStorageDirectory() + "/folder/filename.ext";

if no internal folder use below

如果下面没有使用内部文件夹

 Environment.getExternalStorageDirectory() + "/filename.ext";

#1


8  

First you need to create an imageview.

首先,您需要创建一个imageview。

ImageView imageView = new ImageView(getApplicationContext());

Create layout param to add imageview on layout

创建布局参数以在布局上添加imageview

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Then get your image path

然后获取您的图像路径

String path = Environment.getExternalStorageDirectory() + "/your folder name/image_name.bmp";

Set your image on ImageView

在ImageView上设置图像

Bitmap image = BitmapFactory.decodeFile(path);
imageView.setImageBitmap(image);

Fetch your layout on which you want to add

获取要添加的布局

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout1);

Add your view to the layout

将视图添加到布局中

rl.addView(imageView, lp);

#2


2  

You can use the below code snippet to load image in imageview .

您可以使用以下代码段在imageview中加载图片。

ImageView iv = new ImageView(this);
iv.setImageUri(Uri.parse("file://loaction"));

you can use the above sample method to display image in image from sd card. Even dont hard code the file path use Environment.getExternalEnvironment() to get path.

您可以使用上面的示例方法从SD卡中显示图像中的图像。即使不对文件路径进行硬编码也要使用Environment.getExternalEnvironment()来获取路径。

Environment.getExternalStorageDirectory() + "/folder/filename.ext";

if no internal folder use below

如果下面没有使用内部文件夹

 Environment.getExternalStorageDirectory() + "/filename.ext";