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"目录下,并将其添加到系统的媒体存储库中。

相关内容

热门资讯

一分钟揭秘!aapoker a... 此外,数据分析德州(wepoker如何设置透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
重大通报!wpk官网下载链接,... 重大通报!wpk官网下载链接,newpoker怎么安装脚本,细节揭秘(有挂透视);newpoker怎...
分享给玩家!hhpoker作弊... 1、分享给玩家!hhpoker作弊码,wepokerplus透视脚本免费,德州教程(有挂透明)。2、...
玩家必备攻略!wepoker分... 玩家必备攻略!wepoker分析,约局吧德州透视,微扑克教程(有挂技巧)是一款可以让一直输的玩家,快...
重要通知!wepoker软件安... 重要通知!wepoker软件安装包,epoker免费透视脚本,解密教程(有挂辅助)是一款可以让一直输...
揭秘几款!德州真人透视脚本,w... 揭秘几款!德州真人透视脚本,wepoker私人辅助器,教你教程(有挂方法);大神普及一款德州ai内幕...
分享开挂内幕!pokemmo手... 1、分享开挂内幕!pokemmo手机版脚本,hhpoker透视方法,技巧教程(有挂技巧)(UU po...
科普!wepoker辅助器安装... 1、科普!wepoker辅助器安装包定制,hhpoker德州作弊,2025新版教程(有挂透视)(UU...
总算了解!aapoker脚本,... 总算了解!aapoker脚本,aapoker怎么提高中牌率,切实教程(有挂神器);支持2-10人实时...
揭秘攻略!德普之星透视,hhp... 揭秘攻略!德普之星透视,hhpoker有没有作弊辅助,黑科技教程(有挂技巧);1分钟了解详细教程(微...