昆明阿里云代理商:android 写个软件存储 文件 图片

存储文件:

在 Android 中,可以使用内部存储和外部存储来存储文件。内部存储是应用的私有存储空间,只能应用本身访问。外部存储是 SD 卡等可移动存储设备,可以被多个应用或用户访问。

内部存储:

// 获取内部存储文件路径
File file = getFilesDir();
// 创建文件
File file1 = new File(file, "example.txt");
// 写文件
try {
    FileOutputStream fos = openFileOutput("example.txt", Context.MODE_PRIVATE);
    fos.write("Hello World".getBytes());
    fos.close();
} catch (Exception e) {
    Log.e(TAG, "存储文件失败", e);
}

外部存储:

// 判断 SD 卡是否可用
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    // 获取 SD 卡的公共目录
    File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    // 创建文件
    File file1 = new File(file, "example.txt");
    // 写文件
    try {
        FileOutputStream fos = new FileOutputStream(file1);
        fos.write("Hello World".getBytes());
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, "存储文件失败", e);
    }
} else {
    Log.e(TAG, "SD 卡不可用");
}

存储图片:

// 获取内部存储文件路径
File file = getFilesDir();
// 创建图片文件
File imageFile = new File(file, "example.jpg");

// 从资源中获取图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.example);
// 将 Bitmap 写入图片文件
try {
    FileOutputStream fos = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
} catch (Exception e) {
    Log.e(TAG, "存储图片失败", e);
}

// 从相册选择图片并存储
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(requestCode == 1 && resultCode == RESULT_OK && data != null){
        // 获取选择的图片的 URI
        Uri uri = data.getData();
        // 从 URI 中获取 Bitmap
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将 Bitmap 写入图片文件
        try {
            FileOutputStream fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (Exception e) {
            Log.e(TAG, "存储图片失败", e);
        }
    }
}

Android 写一个软件存储文件和图片的方法如下:

  1. 获取文件和图片的存储路径:

    //获取文件存储路径
    File filePath = getApplicationContext().getFilesDir();
    //获取图片存储路径
    File imagePath = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  2. 创建文件和图片:

    try {
     //创建文件
     File file = new File(filePath,"test.txt");
     file.createNewFile();
     //创建图片
     File imageFile = new File(imagePath,"testImage.jpg");
     imageFile.createNewFile();
    } catch (IOException e) {
     e.printStackTrace();
    }
  3. 将数据写入文件和图片:

    昆明阿里云代理商:android 写个软件存储 文件 图片
    //写入数据到文件
    try {
     FileOutputStream fileOutputStream = openFileOutput("test.txt", Context.MODE_PRIVATE);
     String data = "Hello, World!";
     fileOutputStream.write(data.getBytes());
     fileOutputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    //写入图片到文件
    try {
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
     byte[] byteArray = stream.toByteArray();
     
     FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
     fileOutputStream.write(byteArray);
     fileOutputStream.flush();
     fileOutputStream.close();
     
    } catch (IOException e) {
     e.printStackTrace();
    }
  4. 从文件和图片读取数据:

    //从文件读取数据
    try {
     FileInputStream fileInputStream = openFileInput("test.txt");
     byte[] data = new byte[fileInputStream.available()];
     fileInputStream.read(data);
     fileInputStream.close();
     String text = new String(data);
    } catch (IOException e) {
     e.printStackTrace();
    }
    //从图片读取数据
    try {
     Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
    } catch (Exception e) {
     e.printStackTrace();
    }

    以上是Android写一个存储文件和图片的方法。需要注意的是,对于图片的存储,在使用完后要记得进行释放,以免占用内存过高。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/154790.html

(0)
luotuoemo的头像luotuoemo
上一篇 2024年2月28日 02:51
下一篇 2024年2月28日 03:10

相关推荐

  • 温州阿里云代理商:阿里云安全测评

    阿里云是国内领先的云计算服务提供商,提供各种云计算产品和解决方案。阿里云作为温州地区的代理商,主要提供阿里云产品的销售和技术支持服务。其中,阿里云安全测评是一项针对企业云计算环境进行的安全评估服务。 阿里云安全测评主要包括以下内容: 漏洞扫描和评估:通过对企业云计算环境中可能存在的漏洞进行扫描和评估,发现并修复潜在的安全风险。 网络安全测试:对企业云计算环境…

    2024年1月8日
    22900
  • 郑州阿里云代理商:安装虚拟机电脑很卡

    如果您在安装虚拟机时遇到了电脑卡顿的问题,可以尝试以下解决方法: 确保电脑的配置符合虚拟机的最低系统要求。虚拟机对电脑硬件配置有一定要求,如果您的电脑配置较低,可能会导致运行虚拟机时卡顿。可以查阅虚拟机的系统要求,并与您的电脑配置进行对比。 分配更多的系统资源给虚拟机。在使用虚拟机时,可以考虑增加虚拟机分配的内存、CPU 等资源,以提高虚拟机的运行效果。通过…

    2024年2月16日
    21000
  • 阿里云app ssh工具

    在手机阿里云app中使用ssh连接,用户名应该输入什么?还有密码 你只要用的是linux系统(例如ubuntudebiancentos)就可以用ssh连接,linux下在命令行界面只需要输入命令ssh空格ip然后输入用户名和密码就可以登录,windows下可以使用xshell,putty都行。 如何本地ssh远程登录阿里云服务器 ECS 小鸟云服务器niao…

    2023年8月26日
    24100
  • 澳门阿里云代理商:app模板

    澳门阿里云代理商提供app模板服务,这些模板可以用于开发各种类型的移动应用程序。澳门阿里云代理商的app模板涵盖了多个行业和应用场景,包括电子商务、餐饮、旅游、社交媒体、健身等等。 通过使用澳门阿里云代理商提供的app模板,您可以节省开发时间和成本,快速构建出符合您业务需求的移动应用程序。这些模板通常包含了已经经过优化和测试的功能模块,您可以根据自己的需求进…

    2024年1月17日
    22400
  • 珠海阿里云代理商:aliyun-python-sdk-ossadmin

    珠海阿里云代理商aliyun-python-sdk-ossadmin是一个用于操作阿里云对象存储(OSS)Bucket和Object的Python SDK。通过使用这个SDK,你可以方便地在你的Python应用程序中进行阿里云OSS的管理操作。 主要功能包括: 创建、删除、修改Bucket:可以通过SDK创建、删除和修改阿里云OSS上的Bucket。 文件上…

    2023年12月16日
    21600

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/