获取应用程序外部sd主目录

时间:2013-03-11 12:33:59

标签: java android sd-card

这篇文章是全部编辑的......

我想在sdcard @drawable中保存(/mnt/sdcard/Android/data/app_package/files/)的位图。如果我尝试ContextWrapper.getFilesDirectory(),它将返回"/data/data/app_package/files/"。我想得到“/ mnt / sdcard / Android / data / app_package / files /”。有什么方法可以退货吗?

先谢谢,马泰亚鲁

2 个答案:

答案 0 :(得分:1)

将位图保存在外部存储器中。

首先获取drawable的位图,然后将其保存到SD卡中。

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);

保存SD卡。

public void saveImageToExternalStorage(Bitmap image) {
    //image=scaleCenterCrop(image,200,200);
    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
    try
    {
        File dir = new File(fullPath);
        if (!dir.exists()) {
        dir.mkdirs();
        }
        OutputStream fOut = null;
        File file = new File(fullPath, "photo.png");

        if(file.exists())
            file.delete();

        file.createNewFile();
        fOut = new FileOutputStream(file);
        // 100 means no compression, the lower you go, the stronger the compression
        image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
    catch (Exception e)
    {
        Log.e("saveToExternalStorage()", e.getMessage());
    }
}

答案 1 :(得分:0)

将可绘制目录中的位图存储到外部存储..但我不知道它会如何帮助你

        File f=new File(Environment.getExternalStorageDirectory(),"1.jpg");
        InputStream inputStream =       getResources().openRawResource(R.drawable.the drawable you want to save);
        OutputStream out=new FileOutputStream(f);
        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0)
        out.write(buf,0,len);
        out.close();
        inputStream.close();