资源放在assets文件夹中如何获得其绝对路径【android】

assets文件夹中的文件会随着APP的安装,存放在路径:/data/data/com.example.camera(你的程序包名)/files/文件名
例如:
path=/data/data/com.example.camera/files/liu.jpg
path=/data/data/com.example.camera/files/yz.jpg
asset
获取该路径的方法如下:

//获取工具类
package com.example.camera;
import android.content.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {

    public static String getModelFilePath(Context context, String modelName) {
        copyFileIfNeed(context,modelName);
        return context.getFilesDir().getAbsolutePath() + File.separator + modelName;
    }

    /**
     * 拷贝asset下的文件到context.getFilesDir()目录下
     */
    private static void copyFileIfNeed(Context context, String modelName) {
        InputStream is = null;
        OutputStream os = null;
        try {
            // 默认存储在data/data/<application name>/file目录下
            File modelFile = new File(context.getFilesDir(), modelName);
            is = context.getAssets().open(modelName);
            if (modelFile.length() == is.available()) {
                return;
            }
            os = new FileOutputStream(modelFile);
            byte[] buffer = new byte[1024];
            int length = is.read(buffer);
            while (length > 0) {
                os.write(buffer, 0, length);
                length = is.read(buffer);
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//使用方法:
 String path=FileUtils.getModelFilePath(context, imgName);
 Log.d(TAG,"path="+path);
String path=FileUtils.getModelFilePath(context, "yz.jpg");//获取assets文件夹下名为"yz.jpg"的图片路径
Log.d(TAG,"path="+path);//打印该路径


//运行结果
 path=/data/data/com.example.camera/files/yz.jpg
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Androidassets文件夹是位于应用程序的根目录下的一个目录,它可以用来存放一些静态文件,如html、css、js、图片等。assets文件夹的文件在编译时会被打包到APK,可以通过以下两种方法获取assets文件夹文件的绝对路径。 第一种方法是使用"file:///android_asset/"前缀加上文件名来获取绝对路径。例如,如果你要读取assets文件夹下的一个名为"myfile.txt"的文件,可以使用以下代码来获取其绝对路径: ```java String path = "file:///android_asset/myfile.txt"; ``` 第二种方法是使用InputStream来获取assets文件夹的文件,并将其转换为String类型。可以使用以下代码来获取文件的绝对路径: ```java InputStream abpath = getClass().getResourceAsStream("/assets/myfile.txt"); String path = new String(InputStreamToByte(abpath)); ``` 其,`InputStreamToByte()`方法是将InputStream转换为byte数组的方法,可以使用上述代码提供的方法来实现。 更多关于Android assets文件夹绝对路径的信息,你可以参考以下链接:<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [读写Androidassets目录下的文件的方法详解](https://download.csdn.net/download/weixin_38689055/12797965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android 获取assets绝对路径](https://blog.csdn.net/u014702999/article/details/52413409)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值