2017-09-22 15 views
1
私はフル受け取るために逆に必要しかし
android get thumbnail of image stored on sdcard whose path is known

MediaStore.Imagesは親指ウリ/ IDから完全な画像を得る

ような完全な画像からサムネイルを取得する方法についてのソリューションのカップルがありますが、

サムネイルUri(またはサムネイルID)からの画像Uri。これは現実的に見えた私にURIを返し

fun getGalleryImages(): List<LocalImage> { 
    val baseUri: Uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI 
    val listOfAllImages = ArrayList<LocalImage>() 
    // Set up an array of the Thumbnail Image ID column we want 
    val projection = arrayOf(MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID) 
    // Create the cursor pointing to the SDCard 
    val cursor = context.contentResolver.query(
      baseUri, 
      projection, 
      null, 
      null, 
      null) 

    // Get the column index of the Thumbnails Image ID 
    val thumbColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID) 
    val fullColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID) 

    var thumbnailUri: Uri? 
    while (cursor.moveToNext()) { 
     val thumbId = cursor.getString(thumbColumnIndex) 
     thumbnailUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + thumbId) 
     // here I save image id for later retrieving full image 
     val imageId = cursor.getString(fullColumnIndex) 
     listOfAllImages.add(LocalImage(thumbnailUri = thumbnailUri), imId = imageId) 
    } 
    cursor.close() 
    return listOfAllImages 
} 

そして、私は画像ID(またはサムネイルによるウリ)によって完全な画像を取得する必要が

private fun getFullImage(imageId: String): Uri { 
    val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA) 

    val cursor = context.contentResolver.query(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      projection, 
      MediaStore.Images.Media._ID + "=?", 
      arrayOf(imageId), 
      null) 
    val columnIndex = cursor.getColumnIndex(projection[0]) 
    if (cursor.moveToFirst()) { 
     return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + cursor.getString(1)) 
    } 
    cursor.close() 
    return Uri.EMPTY 
} 

:ここ
は、私は、サムネイルを取得する方法であります

012:

content://media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg 

しかし、ウリは、私はそれからイメージを取得することはできませんので、無効であると思われます

val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, image.imageUri) 

java.lang.IllegalStateException:不明URL:内容:android.os.Parcel.readExceptionで//media/external/images/media//storage/emulated/0/DCIM/Camera/IMG_20170720_085045.jpg ( Parcel.java:1950)android.content.ContentProviderProxy.openTypedAssetFileでandroid.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146) でandroid.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) で(ContentProviderNative。 java:698)

ピカソもこのウリからイメージをロードできません

答えて

-1

イメージを取得した後にこのコードを追加してください。

if (largeImagePath != null) { 

      BitmapFactory.Options opts = new BitmapFactory.Options(); 
      opts.inSampleSize = OG; 
      thumbnail = BitmapFactory.decodeFile((largeImagePath), opts); 
      System.gc(); 
      if (thumbnail != null) { 

       try { 
        thumbnail = Common.rotateImageIfRequired(mContext, thumbnail, Uri.fromFile(new File(largeImagePath))); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       imageCam(thumbnail); 
      } 
     } 

public void imageCam(Bitmap thumbnail) { 
    Bitmap photo = thumbnail; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    photo.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
    byte b[] = bos.toByteArray(); 
    encodedImage = Base64.encodeToString(b, Base64.DEFAULT); 
    ll_preview.setVisibility(View.VISIBLE); 
    img_preview.setVisibility(View.VISIBLE); 
    img_preview.setImageBitmap(photo); 
} 

Common.javaファイルにこのメソッドを追加します。

/** 
    * Rotate an image if required. 
    * 
    * @param img   The image bitmap 
    * @param selectedImage Image URI 
    * @return The resulted Bitmap after manipulation 
    */ 
public static Bitmap rotateImageIfRequired(Context mContext, Bitmap img, Uri selectedImage) throws IOException { 

    InputStream input = mContext.getContentResolver().openInputStream(selectedImage); 
    ExifInterface ei; 
    if (Build.VERSION.SDK_INT > 23) 
     ei = new ExifInterface(input); 
    else 
     ei = new ExifInterface(selectedImage.getPath()); 

    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

    switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      return rotateImage(img, 90); 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      return rotateImage(img, 180); 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      return rotateImage(img, 270); 
     default: 
      return img; 
    } 
} 

私はこれがあなたの問題

+0

がコースを外れ、それは私のために働いソリューションです解決すると思います。 – Dharmishtha

関連する問題