2014-01-21 28 views
9

我有一個應用程序,用戶在其中創建圖像,然後我想將其保存爲從默認圖庫應用程序可見。如何創建應用程序圖像文件夾以在Android圖庫中顯示

現在我不想將照片保存在與照相機拍攝的照片相同的文件夾中,我希望將它們保存在專用於應用程序的文件夾中,就像來自Whatsapp或Facebook等應用程序的圖像一樣。

我試圖拯救他們在這兩個位置中:

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ File.separator + appDirectoryName + File.separator); 

這裏

File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appDirectoryName + File.separator); 

如果我通過手機瀏覽我看到,我成功地保存圖像,但他們不這樣做在畫廊應用程序中顯示。很明顯,我錯過了一些東西,但我不知道它是什麼。也許向文件或文件夾中添加某種元數據,以便圖庫能夠識別它們?

回答

28

那麼我最終找到了答案。

原來是我懷疑的。保存的圖像需要添加一些元數據才能在圖庫中看到(至少在我的設備中)。

這是我做過什麼:

OutputStream fOut = null; 
    File file = new File(imagePath,"GE_"+ System.currentTimeMillis() +".jpg"); 

    try { 
     fOut = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
    try { 
     fOut.flush(); 
     fOut.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    ContentValues values = new ContentValues(); 
    values.put(Images.Media.TITLE, this.getString(R.string.picture_title)); 
    values.put(Images.Media.DESCRIPTION, this.getString(R.string.picture_description)); 
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
    values.put(Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode()); 
    values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US)); 
    values.put("_data", file.getAbsolutePath()); 

    ContentResolver cr = getContentResolver(); 
    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
+0

偉大的解決方案。謝謝 – AlAsiri

+0

@Nemesis嘿夥計,我也在我的應用程序中創建一個類似的流程,除此之外,我需要在圖像查看器中打開圖像。我通過傳遞包含uri單擊圖像的意圖來做到這一點。但它只是顯示一個圖像,有沒有什麼辦法可以從點擊的圖像開始瀏覽指定文件夾中的所有圖像。謝謝 – Calvin

+0

它的工作原理,但不幸的是它減少了將要保存的位圖的大小,但分辨率是相同的。我認爲'bitmap.compress(Bitmap.CompressFormat.JPEG,100,fOut)'是壓縮圖像。 –

2

試試這個

私人無效createDirectoryAndSaveFile(位圖imageToSave,字符串 文件名)

+0

它的工作原理但不幸的是它減少的是將要保存位圖的大小,但是分辨率是相同 –

2

我有同樣的問題{

File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); 

if (!direct.exists()) { 
    File wallpaperDirectory = new File("/sdcard/DirName/"); 
    wallpaperDirectory.mkdirs(); 
    } 

    File file = new File(new File("/sdcard/DirName/"), fileName); 
    if (file.exists()) 
     file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     out.flush(); 
     out.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

}。 第二種方法是正確的方法,但由於畫廊需要刷新,因此您看不到圖庫中的圖像。 所以,你可以等待一段時間,直到它刷新本身,或者您可以使用MediaScanner - 看here

希望這有助於!

+0

您的鏈接無法正常工作,請更新或發佈在這裏示例。 – Paha

2

我做了以下才能正常工作:

public void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED); 
    String mCurrentPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image 
    File file = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(file); 
    mediaScanIntent.setData(contentUri); 
    sendBroadcast(mediaScanIntent); 
} 
0

我想這是完美的作品。

private Uri imageUri; 
String getimgname, getimgpath; 
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File photo = new File(Environment.getExternalStorageDirectory(), "IMG"+System.currentTimeMillis()+".jpg"); 
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo)); 
imageUri = Uri.fromFile(photo); 
getimgname = photo.getName(); 
getimgpath = photo.getAbsolutePath(); 
0

試試這個

MediaScannerConnection.scanFile(context, 
       new String[] { file.toString() }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i("ExternalStorage", "Scanned " + path + ":"); 
         Log.i("ExternalStorage", "-> uri=" + uri); 
        } 
       }); 
相關問題