调用照相机程序,可以在intent中加入android.provider.MediaStore.EXTRA_OUTPUT
参数来设置图片保存位置。如下:
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()
+"/myfavoritepicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
// Uri imageFileUri = Uri.parse("file:///sdcard/myfavoritepicture.jpg");
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_RESULT);
对于大的图片,android加载的时候很有可能出现OOM(out of memory)。这个时候我们需要对图片进行处理。
图片加载到内存所占空间
-------------------------------------------------------------------------------------------
一张分辨率为1024×768,32位色彩的图像,其文件大小约为?
一、基础知识的掌握
首先我们必须明白,图片的每个像素所占的位数。
每个像素的位数,大体有如下几类。
1位,(单色);4位:(16色);8位(256色);
16(64K色,高彩色);24(16M色,真彩色);32(4096M色,增强型真彩色)。
然后,你得明白,在计算机中的最小的存储容量单位为Bit,即位的意思。
二、计算方法
①首先计算出该图片的大小为多少个Bit
拿例题一来计算,就等于:
A、像素总数为:1024x768=786432个像素
B、每个像素占32个Bit。
所以,该图片所占的总的Bit大小为:
1024x768x32=25165824Bit
②计算图片所占的磁盘存储空间
这就需要您对磁盘存储空间单位的换算有一定的掌握。使用总的Bit数转换成为相应的KB、MB、GB等,就可以计算出结果了。
1Byte=8bit 1Kb=1024B 1Mb=1024KB
好了,有了如上的基础,我们就可以计算出该图片的所占的存储空间的大小了。
1024x768x32
现在单位为Bit
1024x768x32
÷8 现在单位为Byte
1024x768x32
÷8
÷1024=3072KB 现在单位为KB
1024x768x32
÷8
÷1024
÷1024
=3MB
现在单位为MB
-------------------------------------------------------
这里可以看出图片加载到内存是很大的,所以我们要对图片进行处理
BitmapFactory.Options可以让我们控制如何将bitmap读入内存中
inSampleSize可以让我们缩放图片
inJustDecodeBounds设置为true,可以让我们不用解析图片就可以得到图片大小。返回的图片对象是null。我们可以得到图片的信息,但是不用为图片分配内存
public class SizedCameraIntent extends Activity {
final static int CAMERA_RESULT = 0;
ImageView mImagevView;
String imageFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ch1);
imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()
+"/myfavoritepicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
// Uri imageFileUri = Uri.parse("file:///sdcard/myfavoritepicture.jpg");
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
mImagevView = (ImageView)findViewById(R.id.returnedImageView);
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
//得到图片大小但是不加载图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, options);
int heightRatio = (int)Math.ceil(options.outHeight/(float)dh);
int widthRatio = (int)Math.ceil(options.outWidth/(float)dw);
if(heightRatio > 1 && widthRatio > 1){
if(heightRatio > widthRatio){
options.inSampleSize = heightRatio;
}else{
options.inSampleSize = widthRatio;
}
}
//解析图片
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, options);
mImagevView.setImageBitmap(bmp);
}
}
}
分享到:
相关推荐
Pro.Android.Media--Android多媒体开发白皮书
org.glassfish.jersey.media:jersey-media-multipart:2.24.1 Jersey 上传文件以及下载文件所需的依赖库,maven下载比较慢,我手动下载下来了。放到你的libs目录
赠送jar包:jersey-media-jaxb-2.22.2.jar; 赠送原API文档:jersey-media-jaxb-2.22.2-javadoc.jar; 赠送源代码:jersey-media-jaxb-2.22.2-sources.jar; 赠送Maven依赖信息文件:jersey-media-jaxb-2.22.2.pom;...
文字识别Ocr Windows.Media.Ocr.Cli-master
android-support-v4.jar android-support-v7-appcompat.jar v7-cardview.jar v7-gridlayout.jar v7-mediarouter.jar v7-palette.jar v7-recyclerview.jar android-support-v13.jar android-support-v17-...
[android.开发书籍].Apress.Pro.Android.Media.Dec.2010
802.3at-2009 IEEE Standard for Information Technology--Part 3: CSMA/CD Access Method and Physical Layer Specifications - Amendment 3: Data Terminal Equipment (DTE) Power via the Media Dependent ...
android 开发书籍 高清 PDF Apress.Pro.Android.Media.Dec.2010
3个jar包: jai_core-1.1.3.jar jai_imageio.jar jai-codec-1.1.3.jar
import javax.media.jai.JAI; import javax.media.jai.RenderedOp; jai_core-1.1.3.jar jai_codec-1.1.3.jar
[android.开发书籍].Apress.Pro.Android.Media.Dec.2010.pdf
android-support-media-compat-26.0.0-alpha1.jar
Specify additions to and appropriate modifications of IEEE Std 802.3 to add greater than 1 Gb/s Physical Layer (PHY) specifications and management parameters for media and operating conditions for ...
【批量下载】[android.开发书籍].Apress.Pro.Android.Media.Dec.2010等
■Chapter 9: Using the Media Framework and Telephony APIs..... 327 ■Chapter 10: Programming 3D Graphics with OpenGL ..... 363 ■Chapter 11: Managing and Organizing Preferences ...... 421 ■Chapter 12...
chromeFOR.COM_vxg-media-player_v1.8.41,支持高版本谷歌播放rtsp的插件--VXG Media Plugin。该插件支持高版本的谷歌浏览器播放rtsp流视频,不必使用42版本的浏览器(低版本浏览器对于很多新的东西支持的并不好),...
This standard specifies the protocol and procedures used to ensure that the synchronization requirements are met for time-sensitive applications, such as audio and ...media where the transmission delays
Plex-Media-Server-1.10.1.4602-f54242b6b
ANSI/IEEE 802.1AB-2005 - IEEE Standard for Local and metropolitan area networks -- Station and Media Access Control Connectivity Discovery
IEEE Std 802 1Qbb 2011 IEEE Standard for Local and metropolitan area networks Media Access Control MAC Bridges and Virtual Bridged Local Area Networks Amendment 17 Priority based Flow Control