简体   繁体   English

使用外部图像作为可绘制资源

[英]Using external image as drawable resource

So I have an image saved on the external SD-Card. 因此,我将图像保存在外部SD卡上。 Now, I want to use that image as a resource in my project. 现在,我想将该图像用作项目中的资源。 (I want to use the image located at the specific path instead of R.drawable.image). (我想使用位于特定路径的图像,而不是R.drawable.image)。

It would be also great, if there was a way to kinda "add" the image to the resources (and not just use it), that the image gets something like a resourceID like the ones in R.drawable got. 如果有一种方法可以将图像“添加”到资源中(而不只是使用它),那图像也将获得类似于resourceID的信息,就像R.drawable中的那样。

But basically I just want to display an external image in an imageView. 但基本上我只想在imageView中显示外部图像。

First you will need read external storage permission 首先,您需要阅读外部存储权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

then, 然后,

String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"directoryName/imageName.png";
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
    ImageView imageView = (ImageView)findViewById(R.id.imageview);
    imageView.setImageBitmap(bitmap);

Use the following code. 使用以下代码。

File file = new File("put your bitmap address here");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

You cannot add any files to the app package (apk file) in runtime. 无法在运行时将任何文件添加到应用程序包(apk文件)中。 If you want the image in your drawable folder, you should add it before packaging. 如果要将图像放在可绘制的文件夹中,则应在打包之前添加它。

String path = "path-to-image";
Bitmap bitmap = BitmapFactory.decodeFile(path);
Drawable drawable = new BitmapDrawable(bitmap); //if you want a drawable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM