Project

General

Profile

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

magiccube / src / main / java / org / distorted / external / RubikFiles.java @ 806329e3

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

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  private RubikFiles()
40
    {
41

    
42
    }
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// PUBLIC API
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public boolean saveFile(Context context, InputStream stream, String name)
57
    {
58
    try
59
      {
60
      File file = new File(context.getFilesDir(), name);
61
      OutputStream outStream = new FileOutputStream(file);
62

    
63
      byte[] buffer = new byte[8*1024];
64
      int bytesRead;
65
      while ((bytesRead = stream.read(buffer)) != -1)
66
        {
67
        outStream.write(buffer, 0, bytesRead);
68
        }
69
      outStream.close();
70

    
71
      return true;
72
      }
73
    catch(IOException ioe)
74
      {
75
      android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
76
      return false;
77
      }
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  public boolean saveFile(Context context, Bitmap bmp, String name)
83
    {
84
    try
85
      {
86
      File file = new File(context.getFilesDir(), name);
87
      OutputStream outStream = new FileOutputStream(file);
88
      bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
89
      outStream.close();
90

    
91
      return true;
92
      }
93
    catch(IOException ioe)
94
      {
95
      android.util.Log.e("D", "Exception trying to save "+name+" : "+ioe.getMessage() );
96
      return false;
97
      }
98
    }
99
  }
(1-1/4)