Extension code unable to read image file in assets?

Come across a problem.

Working on an extension that can read a file from Assets (media folder) or ASD, to convert it to a bitmap. I will expect users of the extension to provide a full path.

This work just fine in companion.

When compiled, the extension code works for an image file in the ASD, but fails with an image file in the Assets. The file path for the image (imgPath) is:

file:///android_asset/icon.png

I am using

String target = new URL(imgPath).toURI().getPath();
(this works fine for me in other extensions)
then

Bitmap bitmap = BitmapFactory.decodeFile(target,options);
Bitmap cBitmap = newCornerBitmap(bitmap,setPixels(cornerRadius),borderWidth,borderColour);
for further processing.

It seems likely that the line to set the variable target cannot handle file:///android_asset/ ?
In testing it returns /android_asset/icon.png

What should I use instead ?

if image is in asset,
copy it to asd

BitmapFactory.decodeFile() won’t work with file:///android_asset/... because assets are inside the APK, not normal filesystem files. You need to open it through the AssetManager and decode the resulting InputStream.

Something like:
String assetPath = imgPath.replace("file:///android_asset/", "");
InputStream is = context.getAssets().open(assetPath);
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
For ASD/external files, you can keep using decodeFile() as you are now. So the main fix is to handle android_asset paths separately rather than converting them to /android_asset/....

1 Like

Thank you both, will consider/try these options for best solution!

Also have to figure out handling urls to files....

This will all keep me busy and happy :slight_smile:

After much fiddling about, eventually got it working using this, by doing a combination of both suggestions, creating a cache file from the asset, then using the path for the cache file :

          try {
			  String assetPath = pathToImage.replace("file:///android_asset/", "");
			  InputStream is = context.getAssets().open(assetPath);

			  File cacheFile = new File(context.getCacheDir(), assetPath);
			  try (FileOutputStream outputStream = new FileOutputStream(cacheFile)) {
				  byte[] buffer = new byte[1024];
				  int length;
				  while ((length = is.read(buffer)) > 0) {
					  outputStream.write(buffer, 0, length);
				  }
			  }
			  String target = cacheFile.getAbsolutePath();
			  BitmapFactory.Options options = new BitmapFactory.Options();
			  options.inScaled = false;
			  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			  Bitmap bitmap = BitmapFactory.decodeFile(target,options);
			  imageView.setImageBitmap(bitmap);
		  } catch (Exception e) {
			  Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
		  }

of course, this won't work in companion!

A post was split to a new topic: How do I set an Image component with an image url?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.