2017-09-21 5 views
0

iam in new、ギャラリーからの画像をUriを使用して表示し、ビットマップを使用して画像を表示することがありますが、画像の読み込みが遅く、アプリケーションをスクロールしても画像が表示されます。 :Android、SQLiteデータベースに画像を保存し、dbから画像を取得する最も良い方法は何ですか?

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

を次のように

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

は、このメソッドを呼び出すと、ストアウリですが、データベース内のイメージを格納し、それを直接表示よりも遅い、それを使用して画像を表示するか、私はから画像を表示するために間違った方法を使用しますギャラリーまたはフォルダ?ギャラリーとデータベースの画像をより良いパフォーマンスで表示するためのより良い方法はありますか?

答えて

1

セキュリティの必要性と、画像をローカルでのみ使用するのか、リモートで使用するのかによって異なります。

あなたはS3にアップロードしてURLをデータベースに保存することができます ギャラリーをメディアストアに保存し、デフォルトの再スキャンではないのでギャラリーを更新するための適切なインテントを呼び出すことができます。

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をイメージに保存するだけです。

希望に役立ちます。

関連する問題