简体   繁体   English

Android,将图像存储到SQLite数据库并从db获取图像的最佳方法是什么?

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

iam new in android , I have an application that display images from Gallery Using Uri , and Display Images Using Bitmap , But Some Times The Images Loaded Slowly and if I Scrolling The App Hanging Although i use standard of converting Uri By Bitmap As Follow : 我是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();
            }
        }
    }

Calling This Method As Follows 如下调用此方法

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

Is Store Uri and Display Images Using it slower than Store The Image In The Database and Display It Directly , Or I Use Wrong Way To Display Images from gallery or folders ? 使用Uri存储和显示图像是否比将图像存储在数据库中并直接显示要慢,还是我使用错误的方式显示图库或文件夹中的图像? is There Better way to display images from gallery and database with better performance? 有没有更好的方法可以更好地显示图库和数据库中的图像?

It depends on your apps need for security and whether the images should be local only or remote. 这取决于您的应用程序对安全性的需求以及图像是仅本地的还是远程的。

You can upload to S3 and then store the url in your Database You can store in mediaStore aka gallery and call the appropriate intents to refresh the gallery as it won't be default rescan, so you have to force a rescan of that file name to add it to the gallery like: 您可以上传到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);
          }
        });

Or you can just save the image to your own private app dir and just store the URI in your database. 或者,您可以将图像保存到自己的私有应用程序目录中,然后将URI存储在数据库中。

The problem with storing in public gallery is that the user can delete it. 存储在公共画廊中的问题是用户可以删除它。 If they delete it and your app loads URIs from your local SQLite and they are missing this will have to be handled as well. 如果他们删除了它,并且您的应用程序从本地SQLite加载了URI,而他们丢失了,则也必须对其进行处理。 Unless you store only in private directory. 除非您仅存储在私有目录中。

My preferred way is "if it is for my app only" then store in private directory for the app, and store URI in the database with the model in a row. 我的首选方式是“如果仅用于我的应用程序”,然后将其存储在应用程序的私有目录中,并将URI与模型中的行一起存储在数据库中。 If it needs to work on other devices then I upload to S3. 如果需要在其他设备上使用,那么我上传到S3。 If it needs to be accessible from gallery then I add to gallery and rescan. 如果需要从图库中访问它,那么我将添加到图库并重新扫描。 But no matter which direction you go, you still just store the URI or URL to the image. 但是,无论您朝哪个方向前进,都仍然只将URI或URL存储到图像中。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-将图像存储到SQLite的最佳方法是什么? - Android - What's the best way to store image to SQLite? 将图像存储在 android 的 Room 数据库中的最佳做法是什么? - What is the Best practice to store the image in Room database in android? android 从手机相册中截取图片存入sqlite数据库 - android take image from phone album and store it in sqlite database 如何在SQLite数据库Android中存储图像或从中获取图像 - How to store and get image into/from a SQLite database android 如何从ImageView检索图像并将其存储在Android的Sqlite数据库中? - How to retrieve an image from an ImageView and store it in an Sqlite Database in Android? 将图片从Android应用存储到Datastore / Google Cloud Storage的最佳方法是什么? - What is the best way to store image from Android app to Datastore/Google Cloud Storage? 将图像存储在应用程序内存中的最佳方法是什么? - What is the best way to store the image in the memory of app? 将图像序列化(与Swing兼容)从Java到Android的最佳方法是什么? - What is the best way to serialize an image (compatible with Swing) from Java to Android? Android Studio-无法从预安装的sqlite数据库检索数据 - Android Studio - unable to retreive data from preinstalled sqlite database 在Android中从URL到ImageView获取图像的最佳方法是什么? - What is the best way to get an image from URL to ImageView in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM