我已经实现了一个按钮来启动相机意图,并允许一个人拍摄照片并将其存储为jpg文件。相机意图打开,我可以拍照,但不会保存:
此行表示:
FileOutputStream fos =新的FileOutputStream(file);
我看了其他页面,更改了清单权限等,但无济于事。
Floating Action Button:
picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ed3.getText()!=null||ed3.getText().toString()!=""||ed3.getText().toString()!=" ") {
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
startActivityForResult(imageIntent, 100);
}
else{
Toast.makeText(MotorControlCentreCheckListTwo.this,"Fill in the first five boxes!",Toast.LENGTH_SHORT).show();
}
}
});
onActivity结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
Bitmap captured = (Bitmap)data.getExtras().get("data");
try {
saveImage(captured);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
保存图像:
private void saveImage(Bitmap captured) throws FileNotFoundException {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root+"/EELTD");
myDir.mkdir();
Bundle bundle =getIntent().getExtras();
int random = 10000;
Random gen = new Random();
random = gen.nextInt(random);
String indent = "";
if (bundle != null) {
indent = getIntent().getStringExtra("uid");
}
String name = indent+random+ed3.getText()+".jpg";
Toast.makeText(this, "Image saved as: "+name, Toast.LENGTH_LONG).show();
File file = new File(myDir,name);
if(file.exists()){
//cancel
}
else{
FileOutputStream fos = new FileOutputStream(file);
captured.compress(Bitmap.CompressFormat.JPEG,90,fos);
String resize = file.getAbsolutePath();
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.joshua.eeltd">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.media.action.IMAGE_CAPTURE"/>
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher_foreground"
android:label="EELTD"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Login.LoginActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Home.HomeActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Form_Options.IdActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Login.RegistrationActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Login.PasswordActivity"
android:screenOrientation="portrait" />
<activity
android:name=".All_Forms.CableSupportContainmentCheckList"
android:screenOrientation="portrait" />
<activity
android:name=".All_Forms.ReadCableSupportContainmentCheckList"
android:screenOrientation="portrait" />
<activity
android:name=".Form_Options.EditForm"
android:label="@string/title_activity_edit_form"
android:theme="@style/AppTheme" />
<activity
android:name=".Form_Options.CleanUp"
android:screenOrientation="portrait" />
<activity
android:name=".Form_Options.ExportForm"
android:screenOrientation="portrait" />
<activity
android:name=".Form_Options.ViewForm"
android:screenOrientation="portrait" />
<activity android:name=".All_Forms.DistributionBoardCheckList" />
<activity android:name=".All_Forms.ReadDistributionBoardCheckList" />
<activity android:name=".All_Forms.EarthingSystemCheckList" />
<activity android:name=".All_Forms.ReadEarthingSystemCheckList" />
<activity android:name=".All_Forms.ElectricalDevicesCheckList" />
<activity android:name=".All_Forms.ReadElectricalDevicesCheckList" />
<activity android:name=".All_Forms.FireAlarmAndSecuritySystemCheckList" />
<activity android:name=".All_Forms.ReadFireAlarmAndSecuritySystemCheckList" />
<activity android:name=".All_Forms.GeneratorInspectionCheckList" />
<activity android:name=".All_Forms.ReadGeneratorInspectionCheckList" />
<activity android:name=".All_Forms.HVCableCheckList" />
<activity android:name=".All_Forms.ReadHVCableCheckList" />
<activity android:name=".All_Forms.HVSwitchgearCheckList" />
<activity android:name=".All_Forms.ReadHVSwitchgearCheckList" />
<activity android:name=".All_Forms.HVTransformerCheckListOil" />
<activity android:name=".All_Forms.ReadHVTransformerCheckListOil" />
<activity android:name=".All_Forms.HVTransformerCheckListDry" />
<activity android:name=".All_Forms.ReadHVTransformerCheckListDry" />
<activity android:name=".All_Forms.LightningProtectionCheckList" />
<activity android:name=".All_Forms.ReadLightningProtectionCheckList" />
<activity android:name=".All_Forms.ScheduleOfLuminaries" />
<activity android:name=".All_Forms.ReadScheduleOfLuminaries" />
<activity android:name=".All_Forms.LVSwitchgearCheckList" />
<activity android:name=".All_Forms.ReadLVSwitchgearCheckList" />
<activity android:name=".All_Forms.MotorControlCentreCheckListOne" />
<activity android:name=".All_Forms.ReadMotorControlCentreCheckListOne" />
<activity android:name=".All_Forms.MotorControlCentreCheckListTwo" />
<activity android:name=".All_Forms.ReadMotorControlCentreCheckListTwo"></activity>
</application>
照相台-单击滴答声以确认照片: [无法附加需要10点声望]
保存的照片带有一个短语“ ad2”,一个随机数和另一个短语“ LF”,使得“ ad200000LF.jpg”,但我找不到该文件,并且举杯告诉我文件名是什么。 提前谢谢!
错误:
Check opening app=AppWindowToken{a17141c token=Token{744498f ActivityRecord{445f0ee u0 com.example.joshua.eeltd/.All_Forms.CableSupportContainmentCheckList t1124}}}: allDrawn=false startingDisplayed=false startingMoved=false isRelaunching()=false
01-13 20:55:54.457 23694-23694/com.example.joshua.eeltd W/System.err: java.io.FileNotFoundException: /storage/emulated/0/EELTD/Ad44620.jpg (No such file or directory)
01-13 20:55:54.457 675-978/? I/HispManager: #Camera# [1]< CHISPManager::powerDown
01-13 20:55:54.457 23694-23694/com.example.joshua.eeltd W/System.err: at java.io.FileOutputStream.open0(Native Method)
01-13 20:55:54.457 675-978/? I/ImagingSystem: #Camera# [860652357]< ImagingSystem_powerDown
01-13 20:55:54.457 23694-23694/com.example.joshua.eeltd W/System.err: at java.io.FileOutputStream.open(FileOutputStream.java:287)
at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at com.example.joshua.eeltd.All_Forms.CableSupportContainmentCheckList.saveImage(CableSupportContainmentCheckList.java:144)
at com.example.joshua.eeltd.All_Forms.CableSupportContainmentCheckList.onActivityResult(CableSupportContainmentCheckList.java:116)
at android.app.Activity.dispatchActivityResult(Activity.java:7684)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4887)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4935)
01-13 20:55:54.458 23694-23694/com.example.joshua.eeltd W/System.err: at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1956)
at android.os.Handler.dispatchMessage(Handler.java:109)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7377)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)