繁体   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