0

这篇文章全部被编辑所以......

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

提前致谢,
Mateiaru

4

2 回答 2

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());
    }
}
于 2013-03-11T12:38:30.053 回答
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();
于 2013-03-11T12:37:08.477 回答