Project

General

Profile

« Previous | Next » 

Revision 806329e3

Added by Leszek Koltunski over 2 years ago

Support for saving the downloaded JSONs in local data storage.

View differences:

src/main/java/org/distorted/external/RubikFiles.java
19 19

  
20 20
package org.distorted.external;
21 21

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

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

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
23 32

  
24 33
public class RubikFiles
25 34
  {
......
44 53

  
45 54
///////////////////////////////////////////////////////////////////////////////////////////////////
46 55

  
47
  public boolean saveFile(InputStream stream, String name)
56
  public boolean saveFile(Context context, InputStream stream, String name)
48 57
    {
49
    return false;
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
      }
50 98
    }
51 99
  }

Also available in: Unified diff