我是android studio中的初学者。我想构建一个相机应用程序,只是拍照并保存到sd卡中,但该应用程序无法正常工作。照片无法保存到手机/ sd卡中。授予xml文件的权限。当我运行我的应用程序时,该应用程序可以拍照但不能保存。 下面是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.my_image);
take_photo = (Button) findViewById(R.id.take_photo);
take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag == 0) {
// -- code for taking photo --
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, requestCode);
} else if(flag == 1) {
//--- code for saving photo ---
savePhotoToMySdCard(b);
Toast.makeText(getApplicationContext(), "Photo saved to sd card!", Toast.LENGTH_SHORT).show();
flag = 0;
take_photo.setText("Take Photo");
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK && data != null){
b = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(b);
flag = 1;
take_photo.setText("Save Photo");
}
}
private void savePhotoToMySdCard(Bitmap bit){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
String pname = sdf.format(new Date());
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File folder = new File(root+"/SCC_Photos");
folder.mkdirs();
File my_file = new File(folder, pname+".jpeg");
if(my_file.exists())
my_file.delete();
try {
FileOutputStream stream = new FileOutputStream(my_file);
bit.compress(Bitmap.CompressFormat.JPEG, 90, stream);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:4)
take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag == 0) {
// -- code for taking photo --
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, requestCode);
} else if(flag == 1) {
int check = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (check != PackageManager.PERMISSION_GRANTED)
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
else {
//--- code for saving photo ---
savePhotoToMySdCard(b);
Toast.makeText(getApplicationContext(), "Photo saved to sd card!", Toast.LENGTH_SHORT).show();
flag = 0;
take_photo.setText("Take Photo");
}
}
}
});
答案 1 :(得分:0)
您可以尝试以下操作:-
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Directory_name");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
if(Build.VERSION.SDK_INT>=24) {
picUri = FileProvider.getUriForFile(UserPhoneNumber.this, getPackageName() + ".provider", image);
}
else {
picUri = Uri.fromFile(image);
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);