繁体   English   中英

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

[英]Proper way how to get image from gallery and captured photos?

不幸的是,我的尝试根本没有用。 奇怪的是,从相机捕获照片可以在调试时使用,但无法在生产中使用。

@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”的from示例时,解码文件最终为空并破坏了所有内容。

从画廊获取和从照片捕获图像的最佳方法是什么?

你应该使用是内容提供商和解析器这里可以看作是数据库和数据库访问更容易理解。

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

因此,如果要访问图库中的所有照片,则应遵循实现内容解析器的指南。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM