小编Mad*_*han的帖子

在 Android Q 中访问外部存储中的照片

我最近将应用程序的目标版本升级到 API 29。由于 Android 10 中的范围存储,我使用 MediaStore API 来存储和检索应用程序外部存储中的图像。之前,我曾经getExternalStoragePublicDirectory存储通过相机拍摄的图像,现在我使用MediaStore.Images.Media.EXTERNAL_CONTENT_URI将文件写入外部存储位置。

我现在面临的问题是,当我打开我的应用程序并拍照时,它存储在我给“myapp”的文件夹名称下,我可以通过 Mediastore 光标检索我的图像并将它们显示在自定义图库中。当我卸载我的应用程序“myapp”文件夹时仍然存在。当我再次安装我的应用程序并尝试从图库中读取图像时,光标没有返回任何图像。但是如果我再次拍照,那么我可以将它们加载到我的自定义图库中。自定义图库视图只是屏幕底部的一行图像,因此用户无需浏览照片文件夹即可将图像加载到应用程序。

这就是我在 MediaStore 中存储图像的方式

内容价值:

String RELATIVE_PATH = Environment.DIRECTORY_PICTURES + File.separator + "myApp";
final ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, generateImageName(new Date()));
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, RELATIVE_PATH);
Run Code Online (Sandbox Code Playgroud)

生成名称方法:

int sameSecondCount;
protected String generateName(Date now)
    {
        String result = formatter.format(now);

        long nowMillis = now.getTime();
        if (nowMillis / 1000 == lastMillis / 1000)
        {
            sameSecondCount++;
            result += "_" + sameSecondCount;
        }
        else
            sameSecondCount = 0;

        lastMillis = nowMillis; …
Run Code Online (Sandbox Code Playgroud)

java android mediastore android-10.0 scoped-storage

8
推荐指数
1
解决办法
3968
查看次数

Android Studio Bumblebee 布局检查器实时渲染不起作用

当我将 Android studio 升级到 Bumblebee 2021.1.1 时,布局检查器实时检查停止工作。补丁2。以前可以用。它显示“活动必须进行硬件加速才能进行实时检查”我为我的活动添加了 hardwareAcceleration 为 true 。但这没有帮助。有人注意到这一点吗? 在此输入图像描述

android android-studio layout-inspector

8
推荐指数
1
解决办法
925
查看次数

getParentFragmentManager 中片段与片段管理器异常不关联

我的照片应用程序中有一个片段。当用户编辑照片时,

  1. 我启动一个 AsyncTask 在后台压缩图像,这将返回压缩的图像字节。
  2. 在 postExecute() 中,我调用 editComplete 方法,该方法将使用压缩图像字节更新我的数据模型
  3. 完成后,我调用 getParentFragmentManager 到 popBackStack 将编辑模式删除为图库模式

在这里,在调用 getParentFragmentManager() 时,我收到 IllegalStateException:“Fragment”+this+“与片段管理器不关联。”

我的片段异步任务:

protected class CompressBitmapImageTask extends AsyncTask<Void, Void, byte[]>
{
    private Bitmap editedImageBitmap;
    private BitmapDownscale bitmapDownscale;

    CompressBitmapImageTask(Bitmap editedImageBitmap, BitmapDownscale bitmapDownscale)
    {
        this.editedImageBitmap = editedImageBitmap;
        this.bitmapDownscale = bitmapDownscale;
    }

    @Override
    protected byte[] doInBackground(Void... params)
    {
        BitmapDownscale.BitmapDownscaleResult result = bitmapDownscale.downscaleFromBitmap(editedBitmap, true);
        return result.bitmapBytes;
    }

    @Override
    protected void onPostExecute(byte[] bytes)
    {
        onEditImageComplete(bytes);
    }
} 
Run Code Online (Sandbox Code Playgroud)
protected void onEditImageComplete(@Nullable byte[] editedBitmapData)
{
    if (editedBitmapData != null)
        photoModel.editedBitmapData = …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

4
推荐指数
1
解决办法
9746
查看次数