Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,将图像存储到SQLite数据库并从db检索图像的最佳方式是什么?_Android_Sqlite - Fatal编程技术网

Android,将图像存储到SQLite数据库并从db检索图像的最佳方式是什么?

Android,将图像存储到SQLite数据库并从db检索图像的最佳方式是什么?,android,sqlite,Android,Sqlite,我是android中的新手,我有一个应用程序,它使用Uri显示图库中的图像,并使用位图显示图像,但有时图像加载速度较慢,如果我滚动应用程序挂起,尽管我使用按位图转换Uri的标准,如下所示: public static Bitmap getBitmapFromUri(Uri uri ,Context context,ImageView imageView) { if (uri == null || uri.toString().isEmpty()) ret

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

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

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

 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。如果需要从gallery访问它,则我将其添加到gallery并重新扫描。但是,无论您选择哪个方向,您仍然只需存储图像的URI或URL

希望有帮助