我用SD解码SD卡的位图BitmapFactory.decodeFile.有时位图大于应用程序需要或堆允许的位图,因此我BitmapFactory.Options.inSampleSize用来请求子采样(较小)位图.
问题是该平台没有强制执行inSampleSize的确切值,有时我的位图太小,或者对于可用内存而言仍然太大.
来自http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:
注意:解码器将尝试满足此请求,但生成的位图可能具有与请求完全相同的不同维度.此外,2的幂通常更快/更容易让解码器兑现.
我应该如何解码SD卡中的位图以获得我需要的确切大小的位图,同时尽可能少地使用内存进行解码呢?
编辑:
当前源代码:
BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
int newWidth = calculateNewWidth(int width, int height);
float sampleSizeF = (float) width / (float) newWidth;
int sampleSize = Math.round(sampleSizeF);
BitmapFactory.Options resample = new BitmapFactory.Options();
resample.inSampleSize = sampleSize; …Run Code Online (Sandbox Code Playgroud) 我想用相机意图制作一张照片并将其保存到默认的DCIM文件夹中.然后我想获得存储图片的路径/文件名.
我正在尝试使用以下代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
Run Code Online (Sandbox Code Playgroud)
使用此代码,相机打开,在拍完一张照片后关闭并将照片保存到默认图像文件夹(通常是/ dcim/camera或sdcard/dcim/camera ......)
但是我怎样才能获得拍摄照片的路径和文件名?我已经试过几乎所有在onActivityResult
我试过
String result = data.getData();
Run Code Online (Sandbox Code Playgroud)
和
String result = data.getDataString();
Run Code Online (Sandbox Code Playgroud)
和
String result = data.toURI();
Run Code Online (Sandbox Code Playgroud)
和
Uri uri = data.getData();
Run Code Online (Sandbox Code Playgroud)
等等
我研究了最近两天为此找到解决方案,网上有很多文章和stackoverflow,但没有任何作用.我不想要缩略图,我只想要路径(uri?)到相机拍摄的图像.
感谢您的任何帮助
编辑:当我尝试:
path=Environment.DIRECTORY_DCIM + "/test.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
Run Code Online (Sandbox Code Playgroud)
它不会将图像存储为test.jpg但是使用正常的图像名称2011-10-03 ..... jpg(但也没关系,我只需要图像的路径,这个名字无关紧要是).
最好的祝福
再次编辑
path=Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
File file = new File(path,"test111111111.jpg");
try {
file.createNewFile();
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud)