2017-10-22 16 views
0

누군가이 문제를 해결할 수 있습니까? 갤러리의 시작 부분에 이미지가 표시되기를 바랍니다. 코드에서 무엇을 변경해야합니까?Android : 이미지는 갤러리 끝에 저장됩니다.

OutputStream fOut = null; 
try { 
    String path = getFilesDir().getPath(); 
    File file = new File(path, "LT-IMG-"+ts+".jpg"); 
    fOut = new FileOutputStream(file); 
    Bitmap bitmap = ((BitmapDrawable)photoView.getDrawable()).getBitmap(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
    fOut.flush(); 
    fOut.close(); 

    MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); 

} catch (IOException ex) { 
    ex.printStackTrace(); 
} 
+0

문제 (오류 메시지가에서 무엇 : 당신이 시작 또는 다른 메타 데이터에 나타나도록 날짜를 수정하려면, 아래의 코드 (SK의 Cortesy, samkirton)를 참조하십시오 stackstrace, 예를 들어) 코드로? 또한 코드를 제공 할 때 최소한의 아직 완전하고 검증 가능한 부분을 제공해야합니다. – kalabalik

답변

0
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription); 

이전 코드 갤러리의 끝 부분에 이미지를 추가합니다.

https://gist.github.com/samkirton/0242ba81d7ca00b475b9

/** 
* Android internals have been modified to store images in the media 
folder with * the correct date meta data * @author samuelkirton*/ 
public class CapturePhotoUtils { 

/** 
* A copy of the Android internals insertImage method, this method populates the 
* meta data with DATE_ADDED and DATE_TAKEN. This fixes a common problem where media 
* that is inserted manually gets saved at the end of the gallery (because date is not populated). 
* @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String, String) 
*/ 
public static final String insertImage(ContentResolver cr, 
     Bitmap source, 
     String title, 
     String description) { 

    ContentValues values = new ContentValues(); 
    values.put(Images.Media.TITLE, title); 
    values.put(Images.Media.DISPLAY_NAME, title); 
    values.put(Images.Media.DESCRIPTION, description); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
    // Add the date meta data to ensure the image is added at the front of the gallery 
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis()); 

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

    try { 
     url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

     if (source != null) { 
      OutputStream imageOut = cr.openOutputStream(url); 
      try { 
       source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut); 
      } finally { 
       imageOut.close(); 
      } 

      long id = ContentUris.parseId(url); 
      // Wait until MINI_KIND thumbnail is generated. 
      Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null); 
      // This is for backward compatibility. 
      storeThumbnail(cr, miniThumb, id, 50F, 50F,Images.Thumbnails.MICRO_KIND); 
     } else { 
      cr.delete(url, null, null); 
      url = null; 
     } 
    } catch (Exception e) { 
     if (url != null) { 
      cr.delete(url, null, null); 
      url = null; 
     } 
    } 

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

    return stringUrl; 
} 

/** 
* A copy of the Android internals StoreThumbnail method, it used with the insertImage to 
* populate the android.provider.MediaStore.Images.Media#insertImage with all the correct 
* meta data. The StoreThumbnail method is private so it must be duplicated here. 
* @see android.provider.MediaStore.Images.Media (StoreThumbnail private method) 
*/ 
private static final Bitmap storeThumbnail(
     ContentResolver cr, 
     Bitmap source, 
     long id, 
     float width, 
     float height, 
     int kind) { 

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

    Uri url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values); 

    try { 
     OutputStream thumbOut = cr.openOutputStream(url); 
     thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut); 
     thumbOut.close(); 
     return thumb; 
    } catch (FileNotFoundException ex) { 
     return null; 
    } catch (IOException ex) { 
     return null; 
    } 
} 
} 
+0

갤러리 앱이 이미지를 표시하는 순서는 갤러리 개발자 (그리고 둘째, 사용자)의 몫입니다. 이 코드는 더 많은 메타 데이터를 제공하며 일부 갤러리 앱이 메타 데이터를 표시하는 방식에 영향을 줄 수 있습니다. 그러나 "이미지가 갤러리 앞쪽에 추가되도록"하지는 않습니다. – CommonsWare

+0

아주 드문 경우이지만 갤러리 개발자에게 달려 있다고 말한대로 반영하지 못할 수도 있습니다. 그러나 그것은 반드시 날짜를 기준으로 이미지를 분류하는 갤러리에서 작동 할 것입니다. –