gpt4 book ai didi

android - 如何从Edittext中获取文字为图片命名

转载 作者:行者123 更新时间:2023-11-30 01:12:35 25 4
gpt4 key购买 nike

我试图让编辑文本来命名已捕获的图片,但是当我尝试时它崩溃了,无论如何都可以这样做吗?如果我这样做就完美了

    String photofile="test"+"jpg";

但不包含编辑文本

public class CameraActivity extends AppCompatActivity implements  SurfaceHolder.Callback {
EditText editText = (EditText)findViewById(R.id.cardnumberbox);


File file_image= getDirc();
if (!file_image.exists() && !file_image.mkdirs()){
Toast.makeText(getApplicationContext(),"Kan ikke lave mappe til at gemme billederne",Toast.LENGTH_SHORT).show();
return;
}
String photofile=editText.getText().toString()+".jpg";
String file_name=file_image.getAbsolutePath()+"/" +photofile;
File picfile=new File(file_name);
try {
outputStream=new FileOutputStream(picfile);
outputStream.write(bytes);
outputStream.close();
} catch (FileNotFoundException e){}
catch (IOException ex) {}
finally {

}

Toast.makeText(getApplicationContext(),"Bilederne er gemt",Toast.LENGTH_SHORT).show();
refreshcamera();
refreshgallery(picfile);
}
};
}
private File getDirc(){
File folder= new File("sdcard");

if (!folder.exists()) {
folder.mkdir();
}
return new File(folder,"pics");
}

07-09 13:14:07.478 3128-3128/camapp.camapp E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{camapp.camapp/camapp.camapp.CameraActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2021) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) at android.app.ActivityThread.access$600(ActivityThread.java:140) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4895) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:72) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183) at camapp.camapp.CameraActivity.(CameraActivity.java:31) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)  at android.app.ActivityThread.access$600(ActivityThread.java:140)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:4895)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)  at dalvik.system.NativeStart.main(Native Method) 

enter image description here

最佳答案

Intent 调用拍照

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
startActivityForResult(cameraIntent, CAMERA_REQUEST);

您将在 onActivityResult() 方法中获取图像

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}

然后将其写入内存

String photofile=editText.getText().toString().trim()+".jpg"; 

// The openfileOutput() method creates a file on the phone/internal storage in the context of your application
final FileOutputStream fos = openFileOutput(photofile, Context.MODE_PRIVATE);

// Use the compress method on the BitMap object to write image to the OutputStream
bm.compress(CompressFormat.JPEG, 90, fos);

或者用它来保存在外部存储上

from developer docs

File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, editText.getText().toString().trim()+".jpg");

阅读它使用

Bitmap bitmap = BitmapFactory.decodeFile(file); 

另外我建议你阅读this answer完整代码。

空指针异常的原因在这些答案中进行了解释,这可能是您代码中的错误,请参阅这些答案

https://stackoverflow.com/a/11470571/5476209

https://stackoverflow.com/a/22476300/5476209

关于android - 如何从Edittext中获取文字为图片命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com