我一直需要得到这个问题的解决方案。我已经从这个社区搜索并测试了许多解决方案,但任何人都适合帮助我。
我有两个活动,第一个拍摄照片并将其发送到另一个有ImageView接收的(直到这里我得到问题)和查询插入文件路径在数据库中(代码做这最后一部分是好的)。
我想这是更好的视图加载图像低分辨率作为缩略图。因此,为了保存到数据库中,我想从一个完整尺寸的图片中获得整个路径。
是否可能的图像必须在一个特定的目录中,我可以选择给函数参数的名称
也许有人知道如何自动设置相机的最小分辨率。
如果你想拥有完整尺寸的图片,你需要在Intent的额外数据上提供URI。这样做,不会返回缩略图,因此您需要生成自己的缩略图。
修改第二个活动,你可以更新你的ImageView与路径保存在你的DB使用onResume()方法的第二个活动;只是要确保它不是空的。
public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private boolean hasCamera(){
return getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
public void takePicture(){
// Check if there is a camera.
if(!hasCamera())
{
Toast.makeText(getActivity(), "This device does not have a camera.", Toast.LENGTH_SHORT).show();
return;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//TODO: Set this to the path you want to save,
//you can use Environment.getExternalStorageDirectory()
//and a TimeStamp to generate an unique name
//Save the path to your DB
Uri exampleUri = Uri.parse("file:///sdcard/photo.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, exampleUri);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved", Toast.LENGTH_LONG).show();
//If you no use onResume(); to update the ImageView
//Another way is calling here your method for update the ImageView
//with the path of your DB
}
else
{
if (resultCode == RESULT_CANCELED)
{
// User cancelled the image capture
// So need to Delete the path from DB
}
}
}
生成缩略图的简单方法:
Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
imageView.setImageBitmap(thumbImage);
获取camera
意图的全尺寸图片和缩略图的示例
首先声明一个按钮来拍照…
Button takePhoto = (Button) findViewById(R.id.btn_takePhoto);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
});
然后在onActivityResult
中我们得到数据…
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
//path from full size image
Cursor cursor = MyActivity.this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION
},
MediaStore.Images.Media.DATE_ADDED,
null,
"date_added DESC");
Bitmap fullsize;
if (cursor != null && cursor.moveToFirst()) {
Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
String photoPath = uri.toString();
cursor.close();
if (photoPath != null) {
System.out.println("path: "+photoPath); //path from image full size
fullsize = decodeSampledBitmap(photoPath);//here is the bitmap of image full size
}
}
// do whatever you want with the Bitmap .... fullsize
// thumbnail
Bitmap thumbnail;
thumbnail = (Bitmap) data.getExtras().get("data");
// do whatever you want with the Bitmap .... thumbnail
}
}
对于内存泄漏使用这个函数
private int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
private Bitmap decodeSampledBitmap(String pathName,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}
//I added this to have a good approximation of the screen size:
private Bitmap decodeSampledBitmap(String pathName) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
return decodeSampledBitmap(pathName, width, height);
}
注意:decodeSampledBitmap是一个功能,以更好的性能在内存泄漏,如当你得到OOM(内存溢出)....
高效加载大型位图