I am trying to capture image and then share it with apps.
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
//start the camera and capture an image
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
//convert captured Image from Intent form to URI
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
//share the image
}
}
But when I run this code, the image doesn't get shared. Is there a bug in my code?
Is there any other way to share an image without saving it in memory?
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
try {
InputStream stream = getContentResolver().openInputStream(screenshotUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
I would like to thank Alexander for helping find the solution.
Here is the working code -
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Intent.EXTRA_STREAM,
Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
(Bitmap) data.getExtras().get("data"), "title", null)
))
.setType("image/jpeg");
startActivity(intent);
}
}
Related
I have added Camera permissions as well which is working perfectly but the image view is not holding the image that is captured. The manifest file is also proper still. The app isn't crashing even it is not showing any errors as well. And I even want to add the image to the database.
public class RiderProfile extends AppCompatActivity {
ImageView imgDp,imgDlFront,imgDlback;
TextView txtDp,txtDl;
Button btnSave;
public static final int CAMERA_REQUEST = 1888;
private static final String TAG = "1111";
private static final int MY_CAMERA_PERMISSION_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rider_profile);
imgDp = (ImageView)findViewById(R.id.imgDp);
imgDlFront = (ImageView)findViewById(R.id.imgDlFront);
imgDlback = (ImageView)findViewById(R.id.imgDlback);
txtDp = (TextView) findViewById(R.id.txtDp);
txtDl = (TextView)findViewById(R.id.txtDl);
btnSave = (Button) findViewById(R.id.btnSave);
imgDp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo1 = (Bitmap) data.getExtras().get("data");
Log.d(TAG, "onActivityResult: click ");
imgDp.setImageBitmap(photo1);
}
}
});
imgDlFront.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo2 = (Bitmap) data.getExtras().get("data");
Log.d(TAG, "onActivityResult: click ");
imgDlFront.setImageBitmap(photo2);
}
}
});
imgDlback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo3 = (Bitmap) data.getExtras().get("data");
Log.d(TAG, "onActivityResult: click ");
imgDlback.setImageBitmap(photo3);
}
}
});
}
}
1. #Override
public void onCameraOpen() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
try {
imageFile = CameraUtils.createImageFile(this);
} catch (IOException e) {
e.printStackTrace();
return;
}
imageUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", imageFile);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(pictureIntent, IntentRequestCode.RESULT_CODE_IMAGE_CAPTURE);
}
}
2. #Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_CODE_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
onCaptureImage(imageFile, imageUri);
} else {
Toast.makeText(this, "Camera canceled", Toast.LENGTH_SHORT).show();
}
break;
}
}
3. void onCaptureImage(File imageFile, Uri imageUri) {
Uri uri = Uri.fromFile(imageFile);
String selectedImagePath = CameraUtils.getPath(application, uri);
File file1 = new File(selectedImagePath);
if (file1.length() != 0) {
FileAttachments b_data = new FileAttachments();
b_data.setFileName(file1.getName());
CameraUtils.writeScaledDownImage(file1, getApplication());
b_data.setFile(file1);
}
}
I implemented this git on my current project without cloning
implementation 'com.github.adityaarora1:LiveEdgeDetection:master-SNAPSHOT'
But I'm unable to call it on my method. The document says
Start startActivityForResult from your activity
startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_CODE);
Get a file path for cropped image on onActivityResult
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
So I tried calling like this onClick button from a new Class
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivity(myIntent);
}
and put the rest inside my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
}
Edit: here is the MainActivity the author used on git I tried using it I get this error:
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
MainActivity (imported)
private static final int REQUEST_CODE = 101;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ScanActivity2.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
}
private void startScan() {
Intent intent = new Intent(this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Update :
After some research I found that the imported project was on read file only and cannot be changed (ScanActivity.java) and my current project was updated sdk 28 which is different from the one Imported so there is some errors in ScanActivity which Is why the button (technically) wasn't working
You should use startActivityForResult instead of startActivity like below.
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
ScanActivity.class);
startActivityForResult(myIntent ,111);
}
});
and modify your onActivityResult like
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == 111){
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
Log.d("YourTAG","File Path "+filePath);
// here you can set bitmap to your image view
yourImageView.setImageBitmap(baseBitmap);
}
}
UPDATE
You Main Activity should be like
private static final int REQUEST_CODE = 111;
private ImageView scannedImageView;
Button scan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
scannedImageView = findViewById(R.id.scanned_image); // this ImageView should be in your activity_scan.xml file with same id(scanned_image)
startScan();
scan = findViewById(R.id.open_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
}
private void startScan() {
Intent intent = new Intent(ScanActivity2.this, ScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
if(null != data && null != data.getExtras()) {
String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
scannedImageView.setImageBitmap(baseBitmap);
}
} else if(resultCode == Activity.RESULT_CANCELED) {
finish();
}
}
}
Demo project
I have uploaded a demo project on Github which is integrated LiveEdgeDetection library and working as expected.
To check it go here
When I try to get the URI on the onActivityResult() method, I get a null pointer exception.
I need to take a picture from the device camera and then pass the URI to the next activity.
That's my actual code:
public void takePicFromCamera(View view){
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "IMG_FOLDER");
imageUri = Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator +
"profile_img.jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 2);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode != 0) {
Uri sel_img = data.getData();
Intent intent = new Intent(MainActivity.this, RateActivity.class);
intent.putExtra("uri", sel_img.toString());
startActivity(intent);
}
if(requestCode == 2 && resultCode != 0){
Intent intent = new Intent(MainActivity.this, RateActivity.class);
intent.putExtra("uri", imageUri.toString());
startActivity(intent);
}
}
The RateActivity after I take the image, doesn't start
This is my code for calling onactivity result. I am using a dialog which prompts to pick image from gallery or from camera. The same code is working in activity but is not working in fragments. I have tried all the previous answers on stackoverflow. Please help
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Pick from Gallery",
"Take from Camera" },
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
try {
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(
intent,
"Complete action using"),
PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
}
break;
case 1:
Intent takePictureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent
.resolveActivity(getActivity()
.getPackageManager()) != null) {
startActivityForResult(
takePictureIntent,
PICK_FROM_CAMERA);
}
break;
default:
break;
}
}
});
builder.show();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
try {
if (requestCode == PICK_FROM_GALLERY) {
System.out.print("ho ja please");
Bundle extras2 = data.getExtras();
if (extras2 != null) {
bitmap = extras2.getParcelable("data");
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
dp.setImageBitmap(output);
}
}
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
Bitmap bitmap1 = (Bitmap) extras.get("data");
bitmap = Bitmap.createScaledBitmap(bitmap1, 250, 250, true);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
dp.setImageBitmap(output);
}
It is because your alert dialog calls Activity.startActivityForResult instead of Fragment.startActivityForResult. If you want to fix that behaviour use the fragment reference inside your dialog to call startActivityForResult.
UPD: also as mentioned #Ashish Tamrakar don't forget to call super.onActivityResult inside your Activity.onActivityResult method if overridden
You can try by calling startActivityForResult with Activity context i.e.
getActivity().startActivityForResult(Intent.createChooser(
intent,
"Complete action using"),
PICK_FROM_GALLERY);
I am using the below code for showing Image Picker, check this if it helps you -
private static final int SELECT_PICTURE = 1; // Declare this variable
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take picture";
// strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent,
pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent });
getActivity().startActivityForResult(chooserIntent,
SELECT_PICTURE);
Then in your Activity you can use this -
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (imageReturnedIntent != null) {
if (imageReturnedIntent.getData() != null) {
Uri selectedImage = imageReturnedIntent.getData(); // use this URI for getting and updating the fragment
}
}
}
How to use default camera to take a picture in android ?
Uri imageUri;
final int TAKE_PICTURE = 115;
public void capturePhoto(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
imageUri = Uri.fromFile(photoFile);
startActivityForResult(intent, TAKE_PICTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = imageUri;
//Do what ever you want
}
}
}
The intent which is used to open the camera is
buttonCapturePhoto.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
The code which gives you the image after capturing is
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uriImage;
InputStream inputStream = null;
if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
uriImage = data.getData();
try {
inputStream = getContentResolver().openInputStream(uriImage);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
imageView.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
}
}
This is a simple example.Anyway this will return the image as a small bitmap.If you want to retrive the full-sized image ,is a bit more complicated.
ImageView takePhotoView = (ImageView) findViewById(R.id.iwTakePicture);
Bitmap imageBitmap = null;
takePhotoView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dispatchTakePictureIntent(0);
}
});
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
this.imageBitmap = (Bitmap) extras.get("data");
takePhotoView.setImageBitmap(imageBitmap);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK)
handleSmallCameraPhoto(data);
}