【问题标题】:Save bitmap canvas as png file in phone internal memory将位图画布保存为手机内存中的 png 文件
【发布时间】:2016-02-02 12:28:09
【问题描述】:

我正在尝试将位图画布绘图保存到手机的内部存储器中,因为我使用的手机没有外部存储器。但我无法在图库或文件资源管理器中看到。在这方面帮助我。 下面是我正在使用的代码。

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/note.png");

Log.i(TAG,"PATH = " +file.getAbsolutePath());

try 
{

mBitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));

} 
catch (Exception e) 
{

e.printStackTrace();

}

【问题讨论】:

  • 你没有关闭文件输出流。

标签: android memory canvas bitmap internal


【解决方案1】:

添加照片后,您需要尝试此代码:

Intent mediaScanIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String currentPath = Environment.getExternalStorageDirectory()
            + "/MyAPP/APP" + n + ".jpg";
    File f = new File(currentPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);

有时会发生图像已添加但设备无法扫描其位置的情况。

【讨论】:

  • 如果我打印路径值,它有/storage/emulated/0/myapp,我认为路径不正确。因此文件没有保存。我的手机没有外部存储器
  • 我可以将图像及其显示在文件夹 SD 卡根目录中。但是如果我用黑色打开图像,它是空白的。
  • 您可能没有指定要保存的正确图像类型,您是否尝试过上面给出的代码?
  • 是的,我试过了。只有这样我才能在 Gallery 中看到图像。如果我检查保存的图像的详细信息,它的大小是 13MB。这意味着它不是空的。
【解决方案2】:

您可以使用设置保存图像的路径 android.os.Environment.getExternalStorageDirectory() 肯定会奏效的。

// for path  to phone's internal memory 
    File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "folder-name");
//for make the directory if not exist 
        if (!cacheDir.exists())
             cacheDir.mkdirs();
    f = new File(cacheDir, "myimage.jpg");
    URL url1 = new URL(your-url);

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2016-01-28
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 2013-06-04
    • 2013-03-26
    • 2023-02-06
    • 2012-11-12
    相关资源
    最近更新 更多