導航:首頁 > 操作系統 > androidbitmap保存本地圖片

androidbitmap保存本地圖片

發布時間:2024-09-26 04:37:09

A. android 讀取Bitmap的幾種方式

想讀取本地項目里的資源圖片,但又不能用到R文件。查了很多資料終於找到了。 現總結以下幾種讀取Bitmap的方法。 1.以文件流的方式,假設在sdcard下有 test.png圖片 FileInputStream fis = new FileInputStream("/sdcard/test.png"); Bitmap bitmap=BitmapFactory.decodeStream(fis); 2. 以R文件的方式,假設 res/drawable下有 test.jpg文件Bitmapbitmap =BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test); 3.以ResourceStream的方式,但不用到R文件。 Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(「/res/drawable/test.png」)); 圖片名就可以讀取到 Bitmap啦。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; //圖片寬高都為原來的二分之一,即圖片為原來的四分一 //以上代碼可以優化內存溢出,但它只是改變圖片大小,並不能徹底解決內存溢出。

B. android中Bitmap存為一張圖片

可以用Bitmap.compress函數來把Bitmap對象保存成PNG或JPG文件,然後可以用BitmapFactory把文件中的數據讀進來再生成Bitmap對象。
保存的代碼大概類似於這樣:
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
具體的可以去查Bitmap和BitmapFactory的幫助文檔。

C. android 已經知道路徑怎麼將路徑中的圖片變成Bitmap

/**
* 獲取本地圖片並指定高度和寬度
*/
public static Bitmap getNativeImage(String imagePath)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 獲取這個圖片的寬和高
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options); //此時返回myBitmap為空
//計算縮放比
int be = (int)(options.outHeight / (float)200);
int ys = options.outHeight % 200;//求余數
float fe = ys / (float)200;
if (fe >= 0.5)
be = be + 1;
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新讀入圖片,注意這次要把options.inJustDecodeBounds 設為 false
options.inJustDecodeBounds = false;
myBitmap = BitmapFactory.decodeFile(imagePath, options);
return myBitmap;
}
/**
* 以最省內存的方式讀取本地資源的圖片 或者SDCard中的圖片
* @param imagePath
* 圖片在SDCard中的路徑
* @return
*/
public static Bitmap getSDCardImg(String imagePath)
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//獲取資源圖片
return BitmapFactory.decodeFile(imagePath, opt);
}

D. android 如何獲取保存的圖片的地址 並存到資料庫中

安卓中如何獲取保存的圖片uri 並保存到sqlite資料庫中
有如下兩種方法,僅供參考
方法一:java代碼

public void saveIcon(Bitmap icon) {
if (icon == null) {
return;
}
// 最終圖標要保存到瀏覽器的內部資料庫中,系統程序均保存為SQLite格式,Browser也不例外,因為圖片是二進制的所以使用位元組數組存儲資料庫的
// BLOB類型
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// 將Bitmap壓縮成PNG編碼,質量為100%存儲
icon.compress(Bitmap.CompressFormat.PNG, 100, os);
// 構造SQLite的Content對象,這里也可以使用
raw ContentValues values = new ContentValues();
// 寫入資料庫的
Browser.BookmarkColumns.TOUCH_ICON欄位 values.put(Browser.BookmarkColumns.TOUCH_ICON, os.toByteArray());
DBUtil.update(....);
//調用更新或者插入到資料庫的方法
}
}

方法二:如果數據表入口時一個content:URIJava代碼

import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing image", e);
}
原文請看http://www.bafenbaosoft.com/post/48.html

E. Android開發中怎麼將照片存儲到系統相冊中

//創建輸出流,指向SD卡的Pictures文件夾
finalFiledirectory=Environment.(Environment.DIRECTORY_DCIM);
//創建保存的文件
Filefile=newFile(directory,"test.png");
FileOutputStreamout=null;
try{
out=newFileOutputStream(file);

}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{

}
//bitmap就是你的圖片
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);

F. Cocos Creator怎麼使用安卓手機相冊中的圖片,最好有示例源碼

android 將drawable中的圖片保存到系統相冊中的原理比較簡單,獲取到的bitmap,然後通過的compress方法寫到一個fileoutputstream中. 再通知MediaScannerService有圖片文件加入就可以了.
保存圖片的核心代碼如下:
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon);
MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, name, "");
或者
FileOutputStream fos = openFileOutput("image", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
//發送系統通知消息
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
另一種方法是直接使用文件流讀寫:
InputStream is = mContext.getResources().openRawResource(PicID);
FileOutputStream fos = new FileOutputStream(LogoFilePath);
byte[] buffer = new byte[8192];
int count = 0;
while((count=is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
這里要注意目錄許可權問題:在應用程序AndroidManifest.xml中的manifest節點中加入android:sharedUerId="android.uid.system"這個屬性。然後放在源碼環境中編譯,並通過adb install 的方式進行安裝。mk文件中的屬性改為LOCAL_CERTIFICATE :=platform。

G. Android調用手機相機拍照,保存圖片會出現2張解決辦法。

最近項目中使用系統相機拍照,保存圖片,發現一些問題。

  讀取圖片旋轉角度,然後再旋轉回去。

   使用BitmapFactory.Options,能更准確的獲取圖片格式,
   判斷地址末尾 .gif 有時候會不準確(不推薦)

  上傳伺服器,一般使用地址,但是用戶手動刪除圖片後,地址是無效的。為了防止地址無效,可以對需要上傳圖片地址做保存,但又希望系統讀取不到,可以對保存地址進行修改。

  讀取圖片地址api

  我們可以去系統相冊查看兩張圖片,會發現兩張圖片的地址是不一樣的,而且兩張圖片的大小也不同。
出現2張的原因是:
 (1)調用系統相機,拍照完成我們會生成一個保存地址,而這個地址是: /storage/android/data/包名/Picture/ ,這張是我們保存的拍照圖片。
 (2)相同的一張圖片在哪?這個地址是:/storage/Pictures/ ,這張圖片是系統復制的App目錄下Pictures中的圖片。

  所以就會出現在系統相冊兩張圖片,但兩張圖片大小不一致,地址不同。

  調用系統api,只能讀取到一張,是系統復制的那張,也就是 /storage/Pictures/ 目錄下的這張,但是/storage/android/data/包名/Picture/ 目錄下的沒有讀取到。

  知道了問題,就有解決辦法,可分為三種方法:
 (1)第一種方法:
     保存圖片的時候,修改下地址(可參照標題 3 ,這樣讓系統無法識別出這張圖片),結果就是,我讀取不到,系統也別想讀取不到,在系統相冊里也就看不到這張圖片。

 (2)第二種方法:
     因為api無法讀取到,那我們就直接再讀取/storage/android/data/包名/Picture/ 下的文件,把圖片一塊加入到同一個集合中用於展示,這樣所有的圖片都有了,但是系統相冊中還是有兩張圖片,為解決這個問題。

 (3)第三種方法:( )
     在我們保存圖片的時候,直接保存到 /storage/Pictures/ 這個目錄下,也不用系統幫我們復制了,這樣就只會出現一張,而且我們也能調用api直接讀取到,兩個問題全都解決了,完美!



(如果以上有錯誤或者有更優美的方式,感謝指出並改之,與君共勉)
這是我項目中出現的問題,希望能夠幫助到你,Thanks,Bye!

閱讀全文

與androidbitmap保存本地圖片相關的資料

熱點內容
學好python要幾個月 瀏覽:193
和平精英玩家邀請程序員 瀏覽:862
本機怎麼通過網頁訪問伺服器 瀏覽:631
東北人的解壓視頻 瀏覽:974
如何學windows編程 瀏覽:405
退出雲伺服器文件夾登陸 瀏覽:687
sqlplus命令不識別怎麼解決 瀏覽:237
雲伺服器如何運行鏡像 瀏覽:542
電腦繞過密碼查看文件夾 瀏覽:666
旅行地圖app在哪裡下載 瀏覽:192
程序員評論自學 瀏覽:120
安卓用什麼軟體導出文件 瀏覽:75
多頭螺紋宏程序編程實例 瀏覽:160
pdfvbnet 瀏覽:91
凌達壓縮機供應鏈 瀏覽:535
hspice命令 瀏覽:733
愛上書app怎麼換語音 瀏覽:839
mc命令下載 瀏覽:612
我的世界國際伺服器怎麼漢化 瀏覽:147
決策模型pdf 瀏覽:595