2016-01-21 76 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); 
}