如何将位图添加到电话库?

问题描述 投票:0回答:1

我正在开发图像编辑App,我需要在Android的Gallery中保存Edited Image,以便用户可以访问并使用它。

我已经尝试通过查看stackoverflow将Bitmap存储到外部存储中。但是像OnePlus5这样的Android手机没有外置存储。所以,我尝试将图像存储在手机的内部存储中使用此代码。哪个看起来很完美,因为它返回Log中存储的Image的路径。在代码位图中是编辑图像的位图。

        ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
        File file = wrapper.getDir("Images", MODE_PRIVATE);
        file = new File(file, "DemoImg2"+ ".jpg");
        try {
            OutputStream stream = null;
            stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();

        }
        catch (IOException e){
            e.printStackTrace();
        }

        Uri savedImagePath = Uri.parse(file.getAbsolutePath());

        Toast.makeText(getApplicationContext(), savedImagePath.toString(), Toast.LENGTH_LONG).show();
        Log.i("Path:", savedImagePath.toString());

但我无法从文件管理器找到/data/user/0/com.packagename.blurd1/app_Images/DemoImg2.jpg。保存的图像也不会出现在图库中的任何位置。在搜索之后,我知道只有app,即存储的图像本身,才能访问存储在内部存储器中的图像,用户无法访问它。那么,即使Android手机没有外部存储,我该怎么办才能将Bitmap Image直接保存到图库中。我在任何地方都找不到任何答案。

android image file bitmap android-gallery
1个回答
2
投票

每个设备都有外部存储。可能是您理解错误,因为Android手机上的外部存储并不意味着存储卡上的内存。它意味着每个电话公司提供的内存,如8GB,16GB,32GB等。因此,您需要将该图像保存在外部存储上,之后您可以将该图像添加到图库中。

请参阅下面的链接,本教程的某些部分可帮助您存储图像并将其添加到图库。

https://developer.android.com/training/camera/photobasics.html

© www.soinside.com 2019 - 2024. All rights reserved.