这个问题已在这个平台上被问到,但没有人帮助过我。 因此,此应用程序有一个按钮单击照片,并回调 onPhotoClicked()。 我在Manifest中提到了 WRITE_EXTERNAL_STORAGE 权限。 点击照片后,取消按钮返回活动,但确定按钮不执行任何操作。
以下是代码:
public class MainActivity extends AppCompatActivity {
Button emailButton, clickPhotoButton;
private static final String FILE_NAME="image01.jpg";
private static final int CAMERA_PIC_REUEST = 100;
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
File pictureDir;
Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailButton = (Button) findViewById(R.id.email_button);
clickPhotoButton = (Button) findViewById(R.id.click_photo_button);
pictureDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Prakhar");
if(!pictureDir.exists()) {
pictureDir.mkdirs();
}
}
public void onPhotoClicked(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(pictureDir, FILE_NAME);
fileUri = Uri.fromFile(image);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_PIC_REUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_PIC_REUEST && resultCode == RESULT_OK) {
ImageView imageView = (ImageView) findViewById(R.id.image_view);
File image = new File(pictureDir, FILE_NAME);
fileUri = Uri.fromFile(image);
imageView.setImageURI(fileUri);
}
}
}