Saving Image array to internal storage and retrieve












0















I searched a lot for image arrays to save locally in the internal phone but could not find any post at all. I am able to save one image to my internal storage and retrieve it in the recycle view of image array but can't save an array of images. also, I am using a database for storing edit text. Please, somebody, explain to me here how can I save and retrieve an array of images with my text from the database here is what I am doing right now



From Mainactivity i am calling loadImageFromStorage in the Util.class



save images from gallery



if (requestCode == GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
try {
bp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), contentURI);
bp = decodeUri(contentURI, 600);
bp = ImageOrientation.modifyOrientation(context.getApplicationContext(), bp ,contentURI);
Util.saveToInternalStorage(context, bp);
arrImages.add(contentURI);


Here I am loading my data from database and images from internal storage



    Notas notas = base.getNota(id);
titulo.setText(notas.getNombre());
if (Util.loadImageFromStorage(context,bp) != null) {
Log.d("TAG_", "bp not null");
bp = Util.loadImageFromStorage(context, bp);
Uri uri = Util.getImageUri(context, bp);
arrImages.add(uri);
rv_images.setAdapter(adapter);
initRecyclerView();
}
SpannableString text = new SpannableString(Html.fromHtml(notas.getTexto()));
texto.setText(text);


The image I am saving and retrieving to recyclerview. These two functions are defined in the Util.class



    public static String saveToInternalStorage(Context context, Bitmap bitmapImage){
String folder_main = "MyImages";
File f = new File(context.getApplicationInfo().dataDir, folder_main);
if (!f.exists()) {
f.mkdirs();
}
// Create imageDir
File mypath = new File(f,"image.jpg");

FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return f.getAbsolutePath();
}

public static Bitmap loadImageFromStorage(Context context, Bitmap bitp) {
String folder_main = "MyImages";
File f = new File(context.getApplicationInfo().dataDir, folder_main);
try {
File z = new File(f.toString(), "image.jpg");
bitp = BitmapFactory.decodeStream(new FileInputStream(z));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return bitp;
}


and last my databasae where I am saving my edit text



  public String insertarNota(Notas letras){
ContentValues values = new ContentValues();
values.put(COLUMN_NOTAS_NOMBRE, letras.getNombre());
values.put(COLUMN_NOTAS_TEXTO, letras.getTexto());
values.put(COLUMN_NOTAS_FECHA, letras.getFecha());

SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NOTAS, null, values);
db.close();
return "exito";
}


public void deleteNota(int id){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NOTAS + " WHERE " + COLUMN_NOTAS_ID + " = '"+id+"' ");
}
public void limpiarTablaNotas(){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NOTAS);
}
public List<Notas> getNotas() {
List<Notas> letras = new ArrayList<>();

String query = "SELECT * FROM " + TABLE_NOTAS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);

Notas letra = null;
if (cursor.moveToFirst()) {
do {
letra = new Notas(cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3));

letras.add(letra);
} while (cursor.moveToNext());
}
cursor.close();

return letras;
}









share|improve this question





























    0















    I searched a lot for image arrays to save locally in the internal phone but could not find any post at all. I am able to save one image to my internal storage and retrieve it in the recycle view of image array but can't save an array of images. also, I am using a database for storing edit text. Please, somebody, explain to me here how can I save and retrieve an array of images with my text from the database here is what I am doing right now



    From Mainactivity i am calling loadImageFromStorage in the Util.class



    save images from gallery



    if (requestCode == GALLERY) {
    if (data != null) {
    Uri contentURI = data.getData();
    try {
    bp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), contentURI);
    bp = decodeUri(contentURI, 600);
    bp = ImageOrientation.modifyOrientation(context.getApplicationContext(), bp ,contentURI);
    Util.saveToInternalStorage(context, bp);
    arrImages.add(contentURI);


    Here I am loading my data from database and images from internal storage



        Notas notas = base.getNota(id);
    titulo.setText(notas.getNombre());
    if (Util.loadImageFromStorage(context,bp) != null) {
    Log.d("TAG_", "bp not null");
    bp = Util.loadImageFromStorage(context, bp);
    Uri uri = Util.getImageUri(context, bp);
    arrImages.add(uri);
    rv_images.setAdapter(adapter);
    initRecyclerView();
    }
    SpannableString text = new SpannableString(Html.fromHtml(notas.getTexto()));
    texto.setText(text);


    The image I am saving and retrieving to recyclerview. These two functions are defined in the Util.class



        public static String saveToInternalStorage(Context context, Bitmap bitmapImage){
    String folder_main = "MyImages";
    File f = new File(context.getApplicationInfo().dataDir, folder_main);
    if (!f.exists()) {
    f.mkdirs();
    }
    // Create imageDir
    File mypath = new File(f,"image.jpg");

    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream(mypath);
    // Use the compress method on the BitMap object to write image to the OutputStream
    bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return f.getAbsolutePath();
    }

    public static Bitmap loadImageFromStorage(Context context, Bitmap bitp) {
    String folder_main = "MyImages";
    File f = new File(context.getApplicationInfo().dataDir, folder_main);
    try {
    File z = new File(f.toString(), "image.jpg");
    bitp = BitmapFactory.decodeStream(new FileInputStream(z));
    }
    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    }
    return bitp;
    }


    and last my databasae where I am saving my edit text



      public String insertarNota(Notas letras){
    ContentValues values = new ContentValues();
    values.put(COLUMN_NOTAS_NOMBRE, letras.getNombre());
    values.put(COLUMN_NOTAS_TEXTO, letras.getTexto());
    values.put(COLUMN_NOTAS_FECHA, letras.getFecha());

    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_NOTAS, null, values);
    db.close();
    return "exito";
    }


    public void deleteNota(int id){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_NOTAS + " WHERE " + COLUMN_NOTAS_ID + " = '"+id+"' ");
    }
    public void limpiarTablaNotas(){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_NOTAS);
    }
    public List<Notas> getNotas() {
    List<Notas> letras = new ArrayList<>();

    String query = "SELECT * FROM " + TABLE_NOTAS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    Notas letra = null;
    if (cursor.moveToFirst()) {
    do {
    letra = new Notas(cursor.getInt(0),
    cursor.getString(1),
    cursor.getString(2),
    cursor.getString(3));

    letras.add(letra);
    } while (cursor.moveToNext());
    }
    cursor.close();

    return letras;
    }









    share|improve this question



























      0












      0








      0








      I searched a lot for image arrays to save locally in the internal phone but could not find any post at all. I am able to save one image to my internal storage and retrieve it in the recycle view of image array but can't save an array of images. also, I am using a database for storing edit text. Please, somebody, explain to me here how can I save and retrieve an array of images with my text from the database here is what I am doing right now



      From Mainactivity i am calling loadImageFromStorage in the Util.class



      save images from gallery



      if (requestCode == GALLERY) {
      if (data != null) {
      Uri contentURI = data.getData();
      try {
      bp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), contentURI);
      bp = decodeUri(contentURI, 600);
      bp = ImageOrientation.modifyOrientation(context.getApplicationContext(), bp ,contentURI);
      Util.saveToInternalStorage(context, bp);
      arrImages.add(contentURI);


      Here I am loading my data from database and images from internal storage



          Notas notas = base.getNota(id);
      titulo.setText(notas.getNombre());
      if (Util.loadImageFromStorage(context,bp) != null) {
      Log.d("TAG_", "bp not null");
      bp = Util.loadImageFromStorage(context, bp);
      Uri uri = Util.getImageUri(context, bp);
      arrImages.add(uri);
      rv_images.setAdapter(adapter);
      initRecyclerView();
      }
      SpannableString text = new SpannableString(Html.fromHtml(notas.getTexto()));
      texto.setText(text);


      The image I am saving and retrieving to recyclerview. These two functions are defined in the Util.class



          public static String saveToInternalStorage(Context context, Bitmap bitmapImage){
      String folder_main = "MyImages";
      File f = new File(context.getApplicationInfo().dataDir, folder_main);
      if (!f.exists()) {
      f.mkdirs();
      }
      // Create imageDir
      File mypath = new File(f,"image.jpg");

      FileOutputStream fos = null;
      try {
      fos = new FileOutputStream(mypath);
      // Use the compress method on the BitMap object to write image to the OutputStream
      bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      try {
      fos.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      return f.getAbsolutePath();
      }

      public static Bitmap loadImageFromStorage(Context context, Bitmap bitp) {
      String folder_main = "MyImages";
      File f = new File(context.getApplicationInfo().dataDir, folder_main);
      try {
      File z = new File(f.toString(), "image.jpg");
      bitp = BitmapFactory.decodeStream(new FileInputStream(z));
      }
      catch (FileNotFoundException e)
      {
      e.printStackTrace();
      }
      return bitp;
      }


      and last my databasae where I am saving my edit text



        public String insertarNota(Notas letras){
      ContentValues values = new ContentValues();
      values.put(COLUMN_NOTAS_NOMBRE, letras.getNombre());
      values.put(COLUMN_NOTAS_TEXTO, letras.getTexto());
      values.put(COLUMN_NOTAS_FECHA, letras.getFecha());

      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_NOTAS, null, values);
      db.close();
      return "exito";
      }


      public void deleteNota(int id){
      SQLiteDatabase db = getWritableDatabase();
      db.execSQL("DELETE FROM " + TABLE_NOTAS + " WHERE " + COLUMN_NOTAS_ID + " = '"+id+"' ");
      }
      public void limpiarTablaNotas(){
      SQLiteDatabase db = getWritableDatabase();
      db.execSQL("DELETE FROM " + TABLE_NOTAS);
      }
      public List<Notas> getNotas() {
      List<Notas> letras = new ArrayList<>();

      String query = "SELECT * FROM " + TABLE_NOTAS;

      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(query, null);

      Notas letra = null;
      if (cursor.moveToFirst()) {
      do {
      letra = new Notas(cursor.getInt(0),
      cursor.getString(1),
      cursor.getString(2),
      cursor.getString(3));

      letras.add(letra);
      } while (cursor.moveToNext());
      }
      cursor.close();

      return letras;
      }









      share|improve this question
















      I searched a lot for image arrays to save locally in the internal phone but could not find any post at all. I am able to save one image to my internal storage and retrieve it in the recycle view of image array but can't save an array of images. also, I am using a database for storing edit text. Please, somebody, explain to me here how can I save and retrieve an array of images with my text from the database here is what I am doing right now



      From Mainactivity i am calling loadImageFromStorage in the Util.class



      save images from gallery



      if (requestCode == GALLERY) {
      if (data != null) {
      Uri contentURI = data.getData();
      try {
      bp = MediaStore.Images.Media.getBitmap(context.getContentResolver(), contentURI);
      bp = decodeUri(contentURI, 600);
      bp = ImageOrientation.modifyOrientation(context.getApplicationContext(), bp ,contentURI);
      Util.saveToInternalStorage(context, bp);
      arrImages.add(contentURI);


      Here I am loading my data from database and images from internal storage



          Notas notas = base.getNota(id);
      titulo.setText(notas.getNombre());
      if (Util.loadImageFromStorage(context,bp) != null) {
      Log.d("TAG_", "bp not null");
      bp = Util.loadImageFromStorage(context, bp);
      Uri uri = Util.getImageUri(context, bp);
      arrImages.add(uri);
      rv_images.setAdapter(adapter);
      initRecyclerView();
      }
      SpannableString text = new SpannableString(Html.fromHtml(notas.getTexto()));
      texto.setText(text);


      The image I am saving and retrieving to recyclerview. These two functions are defined in the Util.class



          public static String saveToInternalStorage(Context context, Bitmap bitmapImage){
      String folder_main = "MyImages";
      File f = new File(context.getApplicationInfo().dataDir, folder_main);
      if (!f.exists()) {
      f.mkdirs();
      }
      // Create imageDir
      File mypath = new File(f,"image.jpg");

      FileOutputStream fos = null;
      try {
      fos = new FileOutputStream(mypath);
      // Use the compress method on the BitMap object to write image to the OutputStream
      bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      try {
      fos.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      return f.getAbsolutePath();
      }

      public static Bitmap loadImageFromStorage(Context context, Bitmap bitp) {
      String folder_main = "MyImages";
      File f = new File(context.getApplicationInfo().dataDir, folder_main);
      try {
      File z = new File(f.toString(), "image.jpg");
      bitp = BitmapFactory.decodeStream(new FileInputStream(z));
      }
      catch (FileNotFoundException e)
      {
      e.printStackTrace();
      }
      return bitp;
      }


      and last my databasae where I am saving my edit text



        public String insertarNota(Notas letras){
      ContentValues values = new ContentValues();
      values.put(COLUMN_NOTAS_NOMBRE, letras.getNombre());
      values.put(COLUMN_NOTAS_TEXTO, letras.getTexto());
      values.put(COLUMN_NOTAS_FECHA, letras.getFecha());

      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_NOTAS, null, values);
      db.close();
      return "exito";
      }


      public void deleteNota(int id){
      SQLiteDatabase db = getWritableDatabase();
      db.execSQL("DELETE FROM " + TABLE_NOTAS + " WHERE " + COLUMN_NOTAS_ID + " = '"+id+"' ");
      }
      public void limpiarTablaNotas(){
      SQLiteDatabase db = getWritableDatabase();
      db.execSQL("DELETE FROM " + TABLE_NOTAS);
      }
      public List<Notas> getNotas() {
      List<Notas> letras = new ArrayList<>();

      String query = "SELECT * FROM " + TABLE_NOTAS;

      SQLiteDatabase db = this.getWritableDatabase();
      Cursor cursor = db.rawQuery(query, null);

      Notas letra = null;
      if (cursor.moveToFirst()) {
      do {
      letra = new Notas(cursor.getInt(0),
      cursor.getString(1),
      cursor.getString(2),
      cursor.getString(3));

      letras.add(letra);
      } while (cursor.moveToNext());
      }
      cursor.close();

      return letras;
      }






      java android arraylist android-image






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 7:34









      Lino

      3,1001828




      3,1001828










      asked Jan 1 at 6:20









      S ShahS Shah

      156111




      156111
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993431%2fsaving-image-array-to-internal-storage-and-retrieve%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993431%2fsaving-image-array-to-internal-storage-and-retrieve%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules