在公共共享存儲上操作圖片的典型代碼示例:

在公共共享存儲上操作圖片的典型代碼示例:
void createExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory.  Note that you should be careful about
    // what you place here, since the user often manages these files.  For
    // pictures and other media owned by the application, consider
    // Context.getExternalMediaDir().

    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");

    try {
        // Make sure the Pictures directory exists.
        path.mkdirs();

        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.

        MediaScannerConnection.scanFile(this,
                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);
            }
        });
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}


//刪除共享內(nèi)存中的圖片
void deleteExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory and delete the file.  If external
    // storage is not currently mounted this will fail.

    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");
    file.delete();
}

//判斷共享內(nèi)存中是否有該圖片
boolean hasExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory and check if the file exists.  If
    // external storage is not currently mounted this will think the
    // picture doesn't exist.
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");
    return file.exists();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末娄蔼,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子项贺,更是在濱河造成了極大的恐慌鳞仙,老刑警劉巖寇蚊,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異繁扎,居然都是意外死亡幔荒,警方通過查閱死者的電腦和手機糊闽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爹梁,“玉大人右犹,你說我怎么就攤上這事∫” “怎么了念链?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長积糯。 經(jīng)常有香客問我掂墓,道長,這世上最難降的妖魔是什么看成? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任君编,我火速辦了婚禮,結(jié)果婚禮上川慌,老公的妹妹穿的比我還像新娘吃嘿。我一直安慰自己,他們只是感情好梦重,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布兑燥。 她就那樣靜靜地躺著,像睡著了一般琴拧。 火紅的嫁衣襯著肌膚如雪降瞳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天蚓胸,我揣著相機與錄音挣饥,去河邊找鬼。 笑死赢织,一個胖子當(dāng)著我的面吹牛亮靴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播于置,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贞岭!你這毒婦竟也來了八毯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瞄桨,失蹤者是張志新(化名)和其女友劉穎话速,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芯侥,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡泊交,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年乳讥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片廓俭。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡云石,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出研乒,到底是詐尸還是另有隱情汹忠,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布雹熬,位于F島的核電站宽菜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏竿报。R本人自食惡果不足惜铅乡,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望烈菌。 院中可真熱鬧阵幸,春花似錦、人聲如沸僧界。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捂襟。三九已至咬腕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間葬荷,已是汗流浹背涨共。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宠漩,地道東北人举反。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像扒吁,于是被迫代替她去往敵國和親火鼻。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理雕崩,服務(wù)發(fā)現(xiàn)魁索,斷路器,智...
    卡卡羅2017閱讀 134,672評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,190評論 25 707
  • 嘰嘰喳喳 小鳥醒來了 床頭邊 衣服盼铁,襪子 運動鞋 叮叮當(dāng)當(dāng) 放學(xué)鈴響了 食堂里 愛吃的粗蔚,不愛吃的 一碗番茄蛋湯 沙...
    力格閱讀 132評論 0 0
  • 戴蒂凡尼的美人魚 文/納蘭饅頭 01 林深是我的老朋友了鹏控,人長得英俊瀟灑致扯,打扮得風(fēng)度翩翩,平時最大興致就是開著他那...
    大故事家閱讀 531評論 0 1
  • 我想你嗎当辐? 不想抖僵! 我想你嗎? 不想瀑构! 我真的不想你嗎裆针? 想
    木兮日記閱讀 126評論 0 1