簡體   English   中英

內部存儲器中保存的位圖不會顯示在圖庫中

[英]Bitmap saved in internal storage doesn't show on Gallery

我創建了一個簡單的應用程序,用於將位圖保存在內部存儲中,並且能夠像其他任何圖像一樣在Android畫廊中顯示該位圖。

問題是:保存后它沒有顯示在圖庫中……而且我不知道自己在做什么錯。 (我正在模擬器上對此進行測試)

這是我處理Bitmap保存的類 當我單擊按鈕保存位圖時,在我的主要活動中使用了這個Saver類。

public class Saver {

private Context mContext;
private String mNameOfFolder = "Storager";
private String mNameOfFile = "Quote";

public void saveImage(Context context, Bitmap imageToSave) {
    mContext = context;

    // Create the directory
    File dir = mContext.getDir(mNameOfFolder, Context.MODE_PRIVATE);
    String filePath = dir.getAbsolutePath();

    // Get the current date and time to attach to the name of the image
    String currentDateAndTime = getCurrentDateAndTime();

    if(!dir.exists()) {
        dir.mkdirs();
    }

    File file = new File(dir, mNameOfFile + currentDateAndTime + ".jpg");

    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);

        out.flush();
        out.close();

        galleryAddPic(file);

        successSave();
    } catch (FileNotFoundException e) {
        Log.d("d", "file not found");
        unableToSave();
    } catch (IOException e) {
        Log.d("d", "IO Exception");
        unableToSave();
    }
}

private void galleryAddPic(File file) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    mContext.sendBroadcast(mediaScanIntent);
}

private String getCurrentDateAndTime() {
    Calendar calendar = Calendar.getInstance();
    // Setting format of the time
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    // Getting the formated date as a string
    String formattedDate = dateFormat.format(calendar.getTime());

    return formattedDate;
}

}

我什至還添加了android開發人員站點在此處提供的galleryAddPic()方法,以便能夠將圖像添加到Media Provider的數據庫中,使其可以在Android Gallery應用程序和其他應用程序中使用。

第三方應用程序(包括MediaStore )無權訪問您應用程序的內部存儲 如果您希望第三方應用訪問該文件,請使用外部存儲

另外,我也不知道為什么要在這里創建ContextWrapper 如果您有需要Context方法(例如,當前代碼中的getDir() ),請在傳遞給您的方法的Context中調用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM