加载和卸载图像

| 如何将图像从SD卡加载和卸载到imageview?     
已邀请:
尝试此操作,假设您知道图像的文件路径:
ImageView img = (ImageView)findViewById(R.id.myimageview);
img.setBackgroundDrawable(Drawable.createFromPath(\"path/to/your/file\"));
    
首先,您需要找到保存/加载文件夹:
public File getDataFolder(Context context) {
    File dataDir = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            dataDir = new File(Environment.getExternalStorageDirectory(), \"myappdata\");
            if(!dataDir.isDirectory()) {
                dataDir.mkdirs();
            }
        }

        if(!dataDir.isDirectory()) {
            dataDir = context.getFilesDir();
        }

    return dataDir;
}
然后,您可以从文件夹中加载图像:
File cacheDir = GlobalClass.instance().getCacheFolder(this);
File cacheFile = new File(cacheDir, wallpaperFilePath);
InputStream fileInputStream = new FileInputStream(cacheFile);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = scale;
bitmapOptions.inJustDecodeBounds = false;
Bitmap wallpaperBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
ImageView imageView = (ImageView)this.findViewById(R.id.preview);
imageView.setImageBitmap(wallpaperBitmap);
您也可以查看此原始示例。它提供了非常有用的功能,可以在SD卡中保存和加载图像。 Android在本地保存并加载下载文件     

要回复问题请先登录注册