簡體   English   中英

使用外部圖像作為可繪制資源

[英]Using external image as drawable resource

因此,我將圖像保存在外部SD卡上。 現在,我想將該圖像用作項目中的資源。 (我想使用位於特定路徑的圖像,而不是R.drawable.image)。

如果有一種方法可以將圖像“添加”到資源中(而不只是使用它),那圖像也將獲得類似於resourceID的信息,就像R.drawable中的那樣。

但基本上我只想在imageView中顯示外部圖像。

首先,您需要閱讀外部存儲權限

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

然后,

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

使用以下代碼。

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

無法在運行時將任何文件添加到應用程序包(apk文件)中。 如果要將圖像放在可繪制的文件夾中,則應在打包之前添加它。

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