2016-01-21 78 views
0

這讓我瘋狂,因爲當我在android中創建一張照片並將其保存在我的摩托羅拉G上時,它在畫廊中顯示正常。但是當我在Samsung Galaxy S3上執行代碼時,它只會出現一個名爲「Camera」的文件夾,其文件類型不明。三星Galaxy S3上不可見的畫廊縮略圖

正如你可以在我的漂亮,意大利麪條代碼中看到,我創建了一個文件:

//Insert an image and create a thumbnail for it. 
// 
// @param cr 
//   The content resolver to use 
// @param source 
//   The stream to use for the image 
// @param title 
//   The name of the image 
// @param description 
//   The description of the image 
// @return The URL to the newly created image, or <code>null</code> if 
//  the image failed to be stored for any reason. 
// 
public final String insertImage(ContentResolver cr, Bitmap source, 
     String title, String description, Long time) { 
    ContentValues values = new ContentValues(); 
    values.put(Images.Media.TITLE, title); 
    values.put(Images.Media.DESCRIPTION, description); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
    values.put(Images.Media.DATE_TAKEN, time); 

    String stringUrl = null; /* value to be returned */ 

    OutputStream imageOut = null; 
    try { 
     valPub.url = cr.insert(Media.EXTERNAL_CONTENT_URI, values); 
     try { 
      imageOut = cr.openOutputStream(valPub.url); 
      valPub.strUltimaFoto = valPub.url.getPath() + "/" + title; 
     } catch (Exception e) { 
      // Esto resuelve el problema de las cámaras Samsung 
      stringUrl = valPub.pathSamsung; 
      File mDir = new File(stringUrl); 
      if (!mDir.exists()) { 
       mDir.mkdirs(); 
      } 
      stringUrl = stringUrl + title; 
      valPub.strUltimaFoto = stringUrl; 
      File m_fil = new File(stringUrl); 
      valPub.url = Uri.fromFile(m_fil); 
      imageOut = new FileOutputStream(m_fil); 
     } 
     try { 
      source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut); 
      // bmpFoto = Bitmap.createBitmap(source); 

     } catch (Exception e) { 
     } finally { 
      imageOut.close(); 
     } 

     long id=0; 
     try { 
      id = ContentUris.parseId(valPub.url); 
     }catch(Exception nEx) { 
      Cursor mCursorLoader; 
      int mColumnIndex; 
      // Initializing CursorLoader 
      mCursorLoader = initializeCursorLoader(); 
      mColumnIndex = mCursorLoader.getColumnIndex(MediaStore.Images.Media._ID); 

      // Go thru all the images in the device (EXTERNAL_CONTENT_URI) 
      for (int i = 0; i < mCursorLoader.getCount(); i++) { 
       mCursorLoader.moveToPosition(i); 
       id = mCursorLoader.getInt(mColumnIndex); 
      } 

     } 

     // Wait until MINI_KIND thumbnail is generated. 
     Bitmap miniThumb = null; 
     try { 
      miniThumb = Images.Thumbnails.getThumbnail(cr, id, 
        Images.Thumbnails.MINI_KIND, null); 
     } catch (Exception e) { 

     } 
     if (miniThumb == null) { 
      // Trick samsung 
      galleryAddPic(valPub.url); 
     } 
     // This is for backward compatibility. 
     @SuppressWarnings("unused") 
     Bitmap microThumb = null; 
     try { 
      microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F, 
        Images.Thumbnails.MICRO_KIND, title); 
     } catch (Exception e) { 
     } 
    } catch (Exception e) {} 

    if (valPub.url != null) { 
     stringUrl = valPub.url.toString(); 
    } 

    return stringUrl; 
} 

我試圖刷新這個畫廊:

public void galleryAddPic(Uri picUri) { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, picUri); 
     sendBroadcast(mediaScanIntent); 
    } 

...但它沒沒有工作。這似乎與某個異步任務有關,但我不知道如何解決它。

其餘代碼:

private Cursor initializeCursorLoader() { 
     String[] COLUMNS = { 
      MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA 
    }; 

    CursorLoader cursorLoader = new CursorLoader(
    this, // Context 
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Uri 
    COLUMNS, // Projection 
    null, // Selection 
    null, // Selection Args 

    // Sort Order: DESC = newest first 
    // Sort Order: ASC = oldest first 

    MediaStore.Images.Media.DATE_ADDED + " DESC"); 

    // *** NOTE *** 
    // With: 
    // 
    // MediaStore.Images.Media.DATE_ADDED + " ASC" 
    // 
    // It runs just fine (MediaStore returns 'null' for invalid thumbnails) 
    // The problem seems to reside on the " DESC" tag. 
    // 
    // How bizarre is that? 

    return cursorLoader.loadInBackground(); 
} 




// public static Uri getContentUri(String volumeName) { 
// return Uri.parse("content://mtp/" + volumeName + "/video/media"); 
// } 

private final Bitmap StoreThumbnail(ContentResolver cr, Bitmap source, 
     long id, float width, float height, int kind, String title) { 
    // create the matrix to scale it 
    Matrix matrix = new Matrix(); 

    float scaleX = width/source.getWidth(); 
    float scaleY = height/source.getHeight(); 

    matrix.setScale(scaleX, scaleY); 

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
      source.getHeight(), matrix, true); 

    ContentValues values = new ContentValues(4); 
    values.put(Images.Thumbnails.KIND, kind); 
    values.put(Images.Thumbnails.IMAGE_ID, (int) id); 
    values.put(Images.Thumbnails.HEIGHT, thumb.getHeight()); 
    values.put(Images.Thumbnails.WIDTH, thumb.getWidth()); 

    valPub.url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values); 
    if (valPub.url == null) { 
     valPub.url = cr.insert(Media.INTERNAL_CONTENT_URI, values); 
    } 
    OutputStream thumbOut = null; 
    try { 
     thumbOut = cr.openOutputStream(valPub.url); 
    } catch (Exception e) { 
    } 
    try { 
     thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut); 
     thumbOut.close(); 
     return thumb; 
    } catch (FileNotFoundException ex) { 
     return null; 
    } catch (IOException ex) { 
     return null; 
    } 
} 

正如我以前說過,這是一個可怕的麪條代碼。我試圖讓它先運行,並且我多次改變它,沒有任何運氣。請隨時提出任何改進建議。

回答

0

最後,我解決了它。 我已更改代碼以執行ALWAYS galleryAdPic,因爲它刷新了三星設備上的圖庫:

public void galleryAddPic(Uri picUri) { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, picUri); 
    sendBroadcast(mediaScanIntent); 
}