java - 如何从图库中获取图像并捕获照片的正确方法?

标签 java android android-bitmap android-gallery

不幸的是,我的尝试根本不起作用。奇怪的是,从相机捕获照片在调试时有效,但在生产中却无效。

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    if (!Debug.isDebuggerConnected()){
        Debug.waitForDebugger();
        Log.d("debug", "started"); // Insert a breakpoint at this line!!
    }

    if (resultCode != RESULT_OK) {
        return;
    }

    File file = null;
    Uri path = null;

    Bitmap image = null;

    switch (requestCode) {
        case RequestCodes.REQUEST_IMAGE_CAPTURE:
            Bundle extras = data.getExtras();
            image = (Bitmap) extras.get("data");
            file = ImageUtils.saveToFile(image, "profile_picture", this);
            mProfileImageView.setImageBitmap(image);
            mCurrentAbsolutePath = file.getAbsolutePath();
            break;
        case RequestCodes.REQUEST_IMAGE_SELECT:
            path = data.getData();
            mProfileImageView.setImageURI(path);
            mCurrentAbsolutePath = path.getPath();
            file = new File(mCurrentAbsolutePath);
            image = BitmapFactory.decodeFile(mCurrentAbsolutePath, new BitmapFactory.Options());
            break;
        default:
            break;
    }

    try {
        if(RequestCodes.REQUEST_IMAGE_SELECT == requestCode){
            file = File.createTempFile(
                    "user_picture",  /* prefix */
                    ".jpeg",         /* suffix */
                    getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
            );
            File pathFile = new File(ImageUtils.getPath(path, this));
            GeneralUtils.copy(pathFile, file);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, 100, 100);
    String thumbnailPath = null;

    // Edited source: https://stackoverflow.com/a/673014/6519101
    FileOutputStream out = null;
    try {
        // PNG is a lossless format, the compression factor (100) is ignored
        thumbnailPath = File.createTempFile(
                "user_picture_thumbnail",  /* prefix */
                ".png",         /* suffix */
                getExternalFilesDir(Environment.DIRECTORY_PICTURES)      /* directory */
        ).getAbsolutePath();
        out = new FileOutputStream(thumbnailPath);
        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    String finalPath = file.getPath();
    UserClient client = new UserClient();
    String finalThumbnailPath = thumbnailPath;
    client.changeUserPicture(file, FileUtils.getMimeType(this, Uri.fromFile(file)), new ApiListener<Response<ResponseBody>>(this){
        @Override
        public void onSuccess(Response<ResponseBody> response, int statusCode) {
            SharedPreferencesManager preferences = SharedPreferencesManager.getInstance();
            preferences.put(SharedPreferencesManager.Key.ACCOUNT_IMAGE_PATH, finalPath);
            preferences.put(SharedPreferencesManager.Key.ACCOUNT_IMAGE_THUMBNAIL_PATH, finalThumbnailPath);
            super.onSuccess(response, statusCode);
        }
    });
}

不幸的是,在调试路径“/0/4/content://media/external/images/media/54257/ORIGINAL/NONE/1043890606”的示例时,解码文件最终为空并破坏了所有内容。

从图库中获取图像并从照片中捕获图像的最佳方式是什么?

最佳答案

您应该使用内容提供程序和解析器 here可以将其视为数据库并访问数据库以便于理解。

该路径称为 URI,本质上类似于数据库条目的链接。相机和图库都积极使用内容提供者和解析器。拍摄照片后,它会被保存,但相机应用程序还会让内容提供商知道已添加新条目。现在,每个具有内容解析器的应用程序(例如图库应用程序)都可以找到该照片,因为 URI 存在。

因此,如果您想访问图库中的所有照片,您应该按照指南来实现内容解析器。

顺便说一句,如果您使用代码复制图像文件但更新了内容提供程序,则您的其他应用程序无法看到新复制的文件,除非它知道绝对路径。但是,当您重新启动手机时,某些系统会对所有图像文件进行全面重新检查,并且您的内容提供商可能会使用新复制的文件进行更新。因此,测试时请尝试重新启动手机。

关于java - 如何从图库中获取图像并捕获照片的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639768/

相关文章:

android - 将 Android Style 与我自己的混合

android - 将图像传递给另一个 Activity 时,图像会丢失其原始结果

android - 错误通知 : The given region must intersect with the Bitmap's dimensions

java - 这个春训有用吗?

java - 使用 Jackson 从 JSON 映射 BasicNameValuePair

java - 如何检查空格后面是否有某个字符?

android - 如何附加存储在本地应用程序目录中的文本文件

android - 当我尝试重复我的操作时,应用程序性能不佳

java - 如何使用 Java 中的方法发送多个值

java - 如何在 Android 中调用 `POST` RESTfull 方法?