将图像数组保存到内部存储并检索

时间:2019-01-01 06:20:32

标签: java android arraylist android-image

我搜索了很多图像数组以保存在内部电话中,但是根本找不到任何帖子。我能够将一个图像保存到我的内部存储中,并在图像阵列的回收视图中检索它,但是不能保存图像阵列。另外,我正在使用数据库来存储编辑文本。请有人在这里向我解释如何保存和检索带有文本的图像数组,这是我现在正在做的事情

从Mainactivity中,我正在Util.class中调用loadImageFromStorage

从图库中保存图像

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);

在这里,我正在从数据库中加载数据,并从内部存储中加载图像

    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);

我要保存并检索到recyclerview的图像。这两个函数在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;
    }

最后一个数据库保存我的编辑文本

  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;
}

0 个答案:

没有答案