簡體   English   中英

Android,將圖像存儲到SQLite數據庫並從db獲取圖像的最佳方法是什么?

[英]Android ,What Is The Best way to store image to SQLite database , and retreive image from db?

我是android中的新手,我有一個應用程序可以使用Uri顯示圖庫中的圖像,並使用位圖顯示圖像,但是有時圖像加載緩慢,如果我滾動應用程序,雖然我使用按位圖轉換Uri的標准如下:

public static Bitmap getBitmapFromUri(Uri uri ,Context context,ImageView imageView) {

        if (uri == null || uri.toString().isEmpty())
            return null;

        // Get the dimensions of the View
        int targetW = imageView.getWidth();
        int targetH = imageView.getHeight();

        InputStream input = null;
        try {
            input = context.getContentResolver().openInputStream(uri);

            // Get the dimensions of the bitmap
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, bmOptions);
            input.close();

            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;

            input = context.getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(input, null, bmOptions);
            input.close();
            return bitmap;

        } catch (FileNotFoundException fne) {
            Log.e(LOG_TAG, "Failed to load image.", fne);
            return null;
        } catch (Exception e) {
            Log.e(LOG_TAG, "Failed to load image.", e);
            return null;
        } finally {
            try {
                input.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

如下調用此方法

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // Receive the request code if match the product request id start get the image Uri
        if (PICK_PRODUCT_IMAGE == requestCode) {
            if (data != null) {
                imageUri = data.getData();

                getContentResolver().takePersistableUriPermission(imageUri,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
                /**
                 * Start Set The Image Bitmap By Uri Of the Image
                 * Using {@link UploadImageBitmap#convertImageUriByBitmap(Uri, Context)}  }
                 */
                  //      productImageView.setImageBitmap(UploadImageBitmap.getBitmapFromUri(imageUri, getApplicationContext(),productImageView));

              productImageView.setImageBitmap(UploadImageBitmap.convertImageUriByBitmap(imageUri, this));

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

使用Uri存儲和顯示圖像是否比將圖像存儲在數據庫中並直接顯示要慢,還是我使用錯誤的方式顯示圖庫或文件夾中的圖像? 有沒有更好的方法可以更好地顯示圖庫和數據庫中的圖像?

這取決於您的應用程序對安全性的需求以及圖像是僅本地的還是遠程的。

您可以上傳到S3,然后將URL存儲在數據庫中。您可以將其存儲在mediaStore中,也可以調用適當的意圖來刷新圖庫,因為它不是默認的重新掃描,因此您必須強制對該文件名進行重新掃描像這樣將其添加到圖庫中:

 MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
        /*
         *   (non-Javadoc)
         * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
         */
        public void onScanCompleted(String path, Uri uri) 
          {
              Log.i("ExternalStorage", "Scanned " + path + ":");
              Log.i("ExternalStorage", "-> uri=" + uri);
          }
        });

或者,您可以將圖像保存到自己的私有應用程序目錄中,然后將URI存儲在數據庫中。

存儲在公共畫廊中的問題是用戶可以刪除它。 如果他們刪除了它,並且您的應用程序從本地SQLite加載了URI,而他們丟失了,則也必須對其進行處理。 除非您僅存儲在私有目錄中。

我的首選方式是“如果僅用於我的應用程序”,然后將其存儲在應用程序的私有目錄中,並將URI與模型中的行一起存儲在數據庫中。 如果需要在其他設備上使用,那么我上傳到S3。 如果需要從圖庫中訪問它,那么我將添加到圖庫並重新掃描。 但是,無論您朝哪個方向前進,都仍然只將URI或URL存儲到圖像中。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM