【问题标题】:saving file on sd card, writes file size 0将文件保存在 sd 卡上,写入文件大小 0
【发布时间】:2012-03-10 03:21:05
【问题描述】:

我无法在 SD 卡上保存图像。我可以在 sd 卡上看到文件,但文件为空(大小为 0)。我尝试将它保存在手机内存中,它工作正常。这是我的代码。

    Imagewritter {

       public static boolean writeAsJPG(Context context, Bitmap bitmap,
    String filename) {
       filename = filename + ".jpg";
       File path = Environment.getExternalStorageDirectory();

    File f = new File(path, filename);
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(f);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(TAG, "file not found");
        return false;
    }

    bitmap.compress(CompressFormat.JPEG, quality, fos);
    try {
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.d(TAG, "error closing");
        e.printStackTrace();
    }
    }

这是位图的来源代码。

    DrawingView = (drawing) findViewById(R.id.drawing_view);
    drawingBitmap = (Bitmap) DrawingView.getBitmap();
    String idName = timeStampText;
    //save Image as JPG
    ImageWritter.writeAsJPG(getApplicationContext(), drawingBitmap, idName);

【问题讨论】:

  • 请贴出实际设置位图变量的代码。另外,不要分配新的 FileOutputStream。由于对 context.openFileOutput 的调用,整个代码块都被浪费了。
  • 谢谢。我将添加位图来自的代码
  • 你是对的。我的代码是多余的。我删除了 context.openFileOutput 并且效果很好。非常感谢

标签: android image file sd-card


【解决方案1】:

我认为你必须在清单文件中添加这些权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案2】:

    获取字节数组中的图像并检查该字节数组的大小..我认为你得到了 0 大小的字节数组.....

    【讨论】:

    • 我不这么认为,因为当我将图像保存在其内部存储器中时。它工作得很好。我的大小没有得到 0。
    【解决方案3】:

    这里唯一的问题是我有两行用于编写文件。使用 FileOutputStream 和 OpenFileOutput。只需删除这些行就可以了。

        try {
            fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d(TAG, "file not found");
            return false;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多