将位图保存到SD卡

4

我有一张Bitmap图像,需要将其存储到SD卡中的一个文件夹中,我的代码如下所示。它创建了预期的文件夹和文件,但是图像没有被存储到文件中,文件仍然为空... 有人可以告诉我错在哪里吗?

Bitmap merged = Bitmap.createBitmap(mDragLayer.getChildAt(0).getWidth(), mDragLayer.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(merged);

// save to folder in sd card
try {
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "folder");
    if(!imagesFolder.exists())
        imagesFolder.mkdirs();
    int imageNum;
    if(imagesFolder.list()==null)
        imageNum = 1;
    else
        imageNum = imagesFolder.list().length + 1;

    String fileName = "file_" + String.valueOf(imageNum) + ".jpg";
    File output = new File(imagesFolder, fileName);
    while(output.exists()){
        imageNum++;
        fileName = "file_" + String.valueOf(imageNum) + ".jpg";
        output = new File(imagesFolder, fileName);
    }

    OutputStream fOut = new FileOutputStream(output);
    merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    fOut.flush();
    fOut.close();

    Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}
3个回答

17

首先在AndroidManifest.xml文件中添加权限

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

然后在Java文件中按以下方式编写。

    String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/MyApp");

            if (!mFolder.exists()) {
                mFolder.mkdir();
            }

            String strF = mFolder.getAbsolutePath();
            File mSubFolder = new File(strF + "/MyApp-SubFolder");

            if (!mSubFolder.exists()) {
                mSubFolder.mkdir();
            }

            String s = "myfile.png";

            f = new File(mSubFolder.getAbsolutePath(),s);

更新

           String strMyImagePath = f.getAbsolutePath();
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(f);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

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

                 e.printStackTrace();
             }

我应该把实际的位图放在哪里? - Jean-Paul Manuel

7
你只需要一个位图,并且需要传递一个存储图像的路径。
Bitmap b = pagesView.getDrawingCache();
b.compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory() + "/NameOfFile.jpg"));

你需要在Manifest文件中添加权限。 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

7

不要用复杂的代码让事情变得困难,实际上很简单,请尝试以下代码。

在您的SD卡中创建第一个目录:

public static String strpath =  android.os.Environment.getExternalStorageDirectory().toString();
public static String dirName = "DIR_NAME";

File makeDirectory = new File(strpath+"/"+dirName);
makeDirectory.mkdir();

那么您需要创建两个字符串变量,如下所示:
String filename =  "yourImageName".jpg";
String dirpath =strpath + "/"+dirName + "/";

创建文件变量:

File storagePath =  new File(dirpath);
File myImage = new File(storagePath, filename);
outStream = new FileOutputStream(myImage);  
outStream.write(data);
outStream.close();

希望这对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接