【问题标题】:Android ,What Is The Best way to store image to SQLite database , and retreive image from db?Android,将图像存储到 SQLite 数据库并从数据库检索图像的最佳方法是什么?
【发布时间】:2018-03-03 04:02:19
【问题描述】:

我在 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 并显示图像是否比将图像存储在数据库中并直接显示它慢,或者我使用错误的方式显示来自画廊或文件夹的图像?有没有更好的方法来显示画廊和数据库中的图像并具有更好的性能?

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    这取决于您的应用程序对安全性的需求以及图像应该是仅本地的还是远程的。

    您可以上传到 S3,然后将 url 存储在您的数据库中 您可以存储在 mediaStore aka 库中并调用适当的意图来刷新库,因为它不会默认重新扫描,因此您必须强制重新扫描该文件名才能将其添加到库中,例如:

     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。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2014-01-29
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 2012-06-04
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多