Building App To Take Photo And Upload But Intent Data Null
I am trying to make a app that takes high quality photo and the upload them but one of the most important part won't work, the camera. Intent data is null so no image a being displ
Solution 1:
I tried this too a couple of days ago and had to struggle a lot. Then I figured it myself.
Here is how my code looks :
1) In onCreate :
capture.setOnClickListener(newView.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)@OverridepublicvoidonClick(View v) {
if ((checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, storagePermissionCode);
}
requestPermissions(newString[]{Manifest.permission.CAMERA}, cameraPermissionCode);
} else {
ContentValuesvalues=newContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
IntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, cameraRequestCode);
}
}
});
2) In onActivityResult :
@RequiresApi(api = Build.VERSION_CODES.M)@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, final Intent data) {
if (requestCode == cameraRequestCode) {
if (resultCode == Activity.RESULT_OK) {
FileOutputStreamstream=null;
try {
Bitmapphoto= MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
StringsaveFilePath= getRealPathFromURI(imageUri);
stream = newFileOutputStream(saveFilePath);
photo.compress(Bitmap.CompressFormat.JPEG, 25, stream);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Can't save image !", Toast.LENGTH_SHORT).show();
} finally {
try {
if (stream != null) {
stream.close();
Toast.makeText(getApplicationContext(), "Image saved successfully !", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Can't save image, try again !", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} elseif (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Can't capture image !", Toast.LENGTH_SHORT).show();
}
}
}
3) getRealPathFromURI function :
publicStringgetRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Post a Comment for "Building App To Take Photo And Upload But Intent Data Null"