Mat*_*att 6 android commonsware
更新可能更改新活动中的位图大小可能会解决问题
String myRef = this.getIntent().getStringExtra("filepath");
File imgFile = new File(myRef);
Log.e("BeatEmUp", myRef);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
myImage.setImageBitmap(myBitmap);
}
Run Code Online (Sandbox Code Playgroud)
这样的事可能吗?
Bitmap scaled = Bitmap.createScaledBitmap(bit, 200, 200, true);
或者这可能是下面最好的方式
到目前为止,我有我的应用程序正在拍照,然后使用一个Intent携带图片并显示在一个新的Activity.一旦我完成了这个,我有一些代码,用顶部显示图像onClick.问题是,当拍摄图像时,我认为这是我的应用程序强行关闭.
在我的logcat中java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB),我认为这意味着图像太大了.
我已经通过在代码中放置一个较小的图像而不是拍摄的相机图像进行测试,这也使我回到问题,即它是相机图片的大小.
所以我试图实现This From CommonsWare但到目前为止没有运气.仔细查看代码,我认为它会搜索最小的resouloution/size,然后使用这些设置拍摄相机我正确地想到了这一点,如果是这样,我怎样才能将其实现到我的代码中?
public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
final int RESULT_SAVEIMAGE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}});
}
ShutterCallback myShutterCallback = new ShutterCallback(){
public void onShutter() {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs(); // <----
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(getBaseContext(), Punch.class);
intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString());
//just using a request code of zero
int request=0;
startActivityForResult(intent,request);
}};
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
camera.release();
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
Camera.Parameters parameters = camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
// This is an undocumented although widely known feature
parameters.set("orientation", "portrait");
// For Android 2.2 and above
camera.setDisplayOrientation(90);
// Uncomment for Android 2.0 and above
parameters.setRotation(90);
} else {
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
camera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
}
camera.startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(previewing && camera != null) {
if(camera!=null) {
camera.stopPreview();
camera.release();
camera = null;
}
previewing = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 13
到目前为止,我有我的应用程序正在拍照,然后使用Intent将图片带到一个新的活动中.
在大多数情况下,使用Intentextras 在活动之间传递数据是一个很好的解决方案.但是,大位图会因内存消耗而导致问题.这是我要小心使用静态数据成员的一种情况.
在我的logcat中我得到java.lang.OutOfMemoryError:位图大小超过VM预算(堆大小= 7431KB,分配= 2956KB,位图大小= 19764KB),我认为这意味着图像太大.
这意味着你没有足够的内存来处理你想做的事情.
仔细查看代码,我认为它会搜索最小的resouloution/size,然后使用这些设置拍摄相机,我认为这是正确的
是.
如果是这样,我如何将其实现到我的代码中?
复制和粘贴通常有效.:-)
告诉相机采取什么大小图片,使用setPictureSize()上Camera.Parameters.但是,您必须选择它支持的大小,并非所有设备都支持任意大小.通话getSupportedPictureSizes()将告诉您支持哪些尺寸.您可以自行决定使用哪种尺寸.在我从其他问题链接的示例中,我迭代这些大小并找到面积最小的大小.
话虽这么说,如果图像大小的唯一问题是将它从活动传递到活动,我会首先进入静态数据成员路由.null当您不再需要图像时,请确保输出该静态数据成员.
这样的事可能吗?
同样,这取决于您的目标是什么.当你只提出一个问题时,你似乎正在寻找解决方案(而且,即使在那里,你也没有告诉我们你在哪里得到内存不足的错误).
如果您的目标是获取全分辨率图片,将其保存在文件中,然后在内存中保留缩略图,这createScaledBitmap()是一个很好的解决方案.
如果您的目标是首先拍摄缩略图(即,您不需要或不需要全分辨率图像),请使用setPictureSize().
如果您的目标是始终使用全分辨率图片,请小心使用静态数据成员来消除任何不必要的副本,Bitmap并查看是否足够.它可能不是,取决于您尝试对图像做什么.
| 归档时间: |
|
| 查看次数: |
15729 次 |
| 最近记录: |