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



这篇文章全部经过编辑,所以…

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

提前感谢,
Mateiaru

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

首先获取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());
    }
}

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

        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();

最新更新