Android 正在sd卡上保存文件,写入文件大小0

Android 正在sd卡上保存文件,写入文件大小0,android,image,file,sd-card,Android,Image,File,Sd Card,我在sd卡上保存图像时遇到问题。我可以在sd卡上看到文件,但文件为空(大小为0)。我试着把它储存在手机内存中,效果很好。这是我的密码 Imagewritter { public static boolean writeAsJPG(Context context, Bitmap bitmap, String filename) { filename = filename + ".jpg"; File path = Environment.

我在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);

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

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


获取字节数组中的图像并检查该字节数组的大小。。我想您得到的是0大小的字节数组….

这里唯一的问题是我有两行代码来编写文件。使用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;
    }

请在位图变量实际设置的位置发布代码。另外,不要分配新的FileOutputStream。由于调用context.openFileOutput,整个代码块都被浪费了。谢谢。我将添加位图的源代码。你说得对。我的代码是多余的。我删除了context.openFileOutput,效果很好。谢谢,我不这么认为,因为当我把图像保存在内存中时。它很好用。我没有得到它的大小0。