我是android studio的新手,我对Java不太满意。
我的问题是,如何将我在MainActivity中创建的函数用于SurfaceView?
我会更好地解释我的情绪:用户拍照。然后它被保存(我的功能onPictureTaken)。但是用户必须输入图像的名称,以便使用其标识符而不是currentTimeMillis进行注册。
在MainActivity中,我检索EditText中输入的内容,但我不知道如何传达这两件事。
我如何获取文字:
public void getText()
{
EditText text = (EditText)findViewById(R.id.imageName);
String nomPhoto = text.getText().toString();
if (nomPhoto.trim().equals("")) {
Toast.makeText(this, "Enter the name of the file", Toast.LENGTH_SHORT).show();
return;
}
}
我如何保存图片:
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
photo = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
//Save pic
if(photo!=null)
{
File file=new File(Environment.getExternalStorageDirectory()+"/dir");
if(!file.isDirectory())
{
file.mkdir();
}
//User must enter the pic name
file = new File(Environment.getExternalStorageDirectory()+"/dir",System.currentTimeMillis()+".jpg");
try
{
FileOutputStream fileOutputStream=new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
fileOutputStream.flush();
System.out.println(file);
fileOutputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
System.out.println("Pic taken");
monTimer mt=new monTimer();
maCamera.release();
maCamera=null;
mt.start();
}
我尝试了这个,但我有一个Java PointerNullException
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
photo = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
//Enregistrer photo sur téléphone
if(photo!=null)
{
File file=new File(Environment.getExternalStorageDirectory()+"/dir");
if(!file.isDirectory())
{
file.mkdir();
}
MainActivity obj = new MainActivity();
String tv = obj.storePhotoName();
file = new File(Environment.getExternalStorageDirectory()+"/dir",tv+".jpg");
也许它与处理程序有关,但我真的不知道
谢谢!
答案 0 :(得分:2)
您可以通过Intent
Intent nextActivity = new Intent(getApplicationContext(), nextActivityName.class);
nextActivity.putExtra("key", "data");
startActivity(nextActivity);
现在在下一个活动中,您可以获取此数据
Intent i = getIntent();
String str = i.getStringExtra("key");
确保您在key
方法中使用的getStringExtra
值与putExtra
方法中使用的{{1}}值相同。
答案 1 :(得分:0)
所以,我刚刚找到答案:
//I get the associate xml file
LinearLayout r = (LinearLayout)this.getParent();
//get the EditText
imageName = r.findViewById(R.id.imageName);
然后,我们需要检查字段是否为空。如果没有,我们可以保存文件:
if (imageName.getText().toString()!=""){
file = new File(Environment.getExternalStorageDirectory()+"/dir",imageName.getText()+".jpg")};