Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从资源加载图像?_Android_Assets - Fatal编程技术网

Android 如何从资源加载图像?

Android 如何从资源加载图像?,android,assets,Android,Assets,我需要从资产中加载一个图像,以避免在某些特定情况下,froyo 2.2.2错误调整POT图像的大小。避免这种情况的方法是从assets dir加载图像文件 我正试图做到这一点: String imagePath = "radiocd5.png"; AssetManager mngr = context.getAssets(); // Create an input stream to read from the asset folder InputStream is = nu

我需要从资产中加载一个图像,以避免在某些特定情况下,froyo 2.2.2错误调整POT图像的大小。避免这种情况的方法是从assets dir加载图像文件

我正试图做到这一点:

String imagePath = "radiocd5.png";
AssetManager mngr = context.getAssets();
// Create an input stream to read from the asset folder
InputStream is = null;
try {
    is = mngr.open(imagePath);
} catch (IOException e1) { e1.printStackTrace();}
    
//Get the texture from the Android resource directory
//InputStream is = context.getResources().openRawResource(R.drawable.radiocd5);
Bitmap bitmap = null;
try {
    //BitmapFactory is an Android graphics utility for images
    bitmap = BitmapFactory.decodeStream(is);

} finally {
    //Always clear and close
    try {
        is.close();
        is = null;
    } catch (IOException e) {}
}
但是我在
is.close()行上得到了NullPointerException


我捕获了一个FileNotFoundException:radiocd5.png,但该文件位于我的资产目录:S中


我做了什么坏事?该文件名为
radiocd5.png
,位于
assets
目录下,而不是使用assets目录,将该文件放入/res/raw,然后您可以使用以下URI访问它:
android。resource://com.your.packagename/“+R.raw.radiocd5

您可以按照我的教程中的说明显示资产中的数据:
加载要显示的图像和文本的示例

我现在添加了所提供链接的相关部分 (在地震或其他情况下);-)泰芬


另一个版本,在片段中:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ root = inflater.inflate(R.layout.createrestaurant_layout, container, false);
  Resources res = getResources();
  img = (ImageView) root.findViewById(R.id.img);
  AssetManager amanager = res.getAssets();
  try {
      InputStream imageStream = amanager.open("restaurant.jpg");
      Drawable drawable = new BitmapDrawable(res, imageStream);
      img.setImageDrawable(drawable);
  }  catch (Exception e) { e.printStackTrace(); }
} 

在iOS/UIKit或iOS/SwiftUI中,此过程似乎要简单得多。

这里是另一种方法

setImageBitmap(BitmapFactory.decodeStream(assets.open("photo.jpg")))

我没有原始目录,我无法创建它,因为此应用程序将由我们公司的生成器自动生成,无法创建目录。什么错误?请澄清,只要您提供足够好的信息,我会尽力帮助您。我捕获了一个文件NotFoundException:radiocd5.png,但该文件位于我的资产目录:Sa good practice,无论何时更新资源,清理项目并再次运行!图像加载后,输入流不也应该关闭吗?在第50行之后。你能在你的回答中提供一些代码和说明吗。链接回答是不鼓励的,尤其是其他网站可能会宕机(earthqake或一些你永远不知道的东西:P)
    protected String openImageInAssets(String imageName){
        String encodedImageBase64 = "";
        AssetManager assetManager = getAssets();
        InputStream fileStream = null;
        try {
            fileStream = assetManager.open(imageName);
            if(fileStream != null){
                //                  BitmapFactory.Options bfo = new BitmapFactory.Options();
                //                  bfo.inPreferredConfig = Bitmap.Config.ARGB_8888;
                //                  Bitmap bitmap = BitmapFactory.decodeStream(fileStream, null, bfo);

                Bitmap bitmap = BitmapFactory.decodeStream(fileStream);
                // Convert bitmap to Base64 encoded image for web
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                // to get image extension file name split the received
                int fileExtensionPosition = imageName.lastIndexOf('.');
                String fileExtension = imageName.substring(fileExtensionPosition+1);
                //                  Log.d(IConstants.TAG,"fileExtension: " + fileExtension);

                if(fileExtension.equalsIgnoreCase("png")){
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    //                      Log.d(IConstants.TAG,"fileExtension is PNG");
                }else if(fileExtension.equalsIgnoreCase("jpg") || fileExtension.equalsIgnoreCase("jpeg")){
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    //                      Log.d(TAG,"fileExtension is JPG");
                }

                byte[] byteArray = byteArrayOutputStream.toByteArray();
                String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                encodedImageBase64 = "data:image/png;base64," + imgageBase64;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return encodedImageBase64="";
        }
        finally {
            //Always clear and close
            try {
                if(fileStream != null) {
                    fileStream.close();
                    fileStream = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

//      Log.d(TAG,"encodedImageBase64: " + encodedImageBase64);
        return encodedImageBase64;
    }
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ root = inflater.inflate(R.layout.createrestaurant_layout, container, false);
  Resources res = getResources();
  img = (ImageView) root.findViewById(R.id.img);
  AssetManager amanager = res.getAssets();
  try {
      InputStream imageStream = amanager.open("restaurant.jpg");
      Drawable drawable = new BitmapDrawable(res, imageStream);
      img.setImageDrawable(drawable);
  }  catch (Exception e) { e.printStackTrace(); }
} 
setImageBitmap(BitmapFactory.decodeStream(assets.open("photo.jpg")))