Project

General

Profile

Download (4.31 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / external / RubikFiles.java @ 7fe62d1f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.external;
21

    
22
import java.io.File;
23
import java.io.FileOutputStream;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27

    
28
import android.content.Context;
29
import android.graphics.Bitmap;
30
import android.graphics.BitmapFactory;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class RubikFiles
35
  {
36
  private static RubikFiles mThis;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
  private RubikFiles()
41
    {
42

    
43
    }
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
// PUBLIC API
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  public static RubikFiles getInstance()
50
    {
51
    if( mThis==null ) mThis = new RubikFiles();
52
    return mThis;
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  public Bitmap getIcon(Context context, String name)
58
    {
59
    File file = new File(context.getFilesDir(), name);
60

    
61
    if( file.exists() )
62
      {
63
      String fname = context.getFilesDir().getAbsolutePath()+"/"+name;
64
      return BitmapFactory.decodeFile(fname);
65
      }
66

    
67
    return null;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  public boolean saveFile(Context context, InputStream stream, String name)
73
    {
74
    try
75
      {
76
      File file = new File(context.getFilesDir(), name);
77
      OutputStream outStream = new FileOutputStream(file);
78

    
79
      byte[] buffer = new byte[8*1024];
80
      int bytesRead;
81
      while ((bytesRead = stream.read(buffer)) != -1)
82
        {
83
        outStream.write(buffer, 0, bytesRead);
84
        }
85
      outStream.close();
86

    
87
      return true;
88
      }
89
    catch(IOException ioe)
90
      {
91
      android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
92
      return false;
93
      }
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public boolean saveIcon(Context context, Bitmap bmp, String name)
99
    {
100
    try
101
      {
102
      File file = new File(context.getFilesDir(), name);
103
      OutputStream outStream = new FileOutputStream(file);
104
      bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
105
      outStream.close();
106

    
107
      return true;
108
      }
109
    catch(IOException ioe)
110
      {
111
      android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
112
      return false;
113
      }
114
    }
115
  }
(1-1/4)