导航:首页 > 操作系统 > 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保存本地图片相关的资料

热点内容
和平精英玩家邀请程序员 浏览: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
怎么下载市长热线app 浏览:152