Android相机保存图像,但不确定保存在哪里。
创始人
2024-10-12 12:30:23
0

在Android上保存图像通常会使用相机应用程序提供的默认位置,但您也可以指定保存图像的位置。以下是一个示例,演示如何保存图像并获取保存的文件路径。

import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ImageUtils {

    private static final String TAG = "ImageUtils";

    public static String saveImageToGallery(ContentResolver contentResolver, Bitmap bitmap) {
        String fileName = generateFileName();
        String filePath = null;

        try {
            // Save the image to external storage
            File imageFile = createImageFile(fileName);
            OutputStream outputStream = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();

            // Add the image to the media store
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            values.put(MediaStore.Images.Media.DATA, imageFile.getAbsolutePath());
            Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            if (uri != null) {
                filePath = uri.getPath();
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to save image: " + e.getMessage());
        }

        return filePath;
    }

    private static String generateFileName() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        return "IMG_" + timeStamp + ".jpg";
    }

    private static File createImageFile(String fileName) {
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return new File(storageDir, fileName);
    }
}

要保存图像,请调用saveImageToGallery(ContentResolver, Bitmap)方法,并传入ContentResolver对象和要保存的图像Bitmap。该方法将返回保存的图像文件路径。

请确保在AndroidManifest.xml文件中添加以下权限:


此代码示例将图像保存在外部存储的"Pictures"目录下,并将其添加到系统的媒体存储库中。

相关内容

热门资讯

wepoker免费脚本弱密码!... wepoker免费脚本弱密码!wepoker底牌透视脚本(透视)软件-一贯教你是有挂1、操作简单,无...
透视详情!poker红龙辅助(... 透视详情!poker红龙辅助(透视)wepoker辅助软件视频,教程模块(有挂技巧)-哔哩哔哩wep...
wpk透视最怕三个东西!hhp... wpk透视最怕三个东西!hhpoker辅助(透视)工具-确实了解是有挂wpk透视最怕三个东西破解侠是...
透视教你!wepoker祈福有... 透视教你!wepoker祈福有用吗(透视)智星德州插件,教程方针(有挂详细)-哔哩哔哩1、上手简单,...
hhpoker免费透视脚本!德... hhpoker免费透视脚本!德普之星辅助器怎么用(透视)器-总是关于存在有挂1、上手简单,内置详细流...
透视必备!竞技联盟破解版最新版... 透视必备!竞技联盟破解版最新版(透视)德州透视插件,教程法门(有挂攻略)-哔哩哔哩1、全新机制【竞技...
佛手大菠萝13道挂哪里!hhp... 佛手大菠萝13道挂哪里!hhpoker透视脚本(透视)app-原来教你真的是有挂1、在佛手大菠萝13...
透视揭露!wepoker养号规... 透视揭露!wepoker养号规律(透视)竞技联盟透视插件,教程指引(讲解有挂)-哔哩哔哩1、进入到竞...
wepoker软件靠谱么!竞技... wepoker软件靠谱么!竞技联盟透视(透视)方法-真是曝光是有挂1、不需要AI权限,帮助你快速的进...
透视揭幕!hhpoker有没有... 透视揭幕!hhpoker有没有透视挂(透视)wepoker到底有没有透视,教程秘籍(有挂透视)-哔哩...