I call this function:
private void TakePhoto() {
LogService.log(TAG, "inTakePicture");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/BlueSkyBio/media/", "test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
Which takes me on the next onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if(outputFileUri != null){
LogService.log("MainFragment", outputFileUri.toString());
String path = outputFileUri.toString();
selectedVideoPath = path.substring(7);
LogService.log("in take pic", "selectedImagePath: " + selectedVideoPath);
Intent paintActivity = new Intent(getActivity(), PaintActivity.class);
paintActivity.putExtra("selectedImagePath", selectedVideoPath);
paintActivity.putExtra("isVideo", false);
startActivity(paintActivity);
((FragmentActivity) getActivity()).finish();
} else{
// Toast.makeText(getActivity(), "No picture taken", Toast.LENGTH_SHORT).show();
Intent main = new Intent(getActivity(), FragmentActivity.class);
startActivity(main);
((FragmentActivity) getActivity()).finish();
}
}
}
This works ok, but if i call the intent to take a picture, and then press the back button, if i already took a picture before, it will load that picture, if not, it will crash, because by pressing back, it will not take a picture. What can i do to escape this situation?
I have tried to test:
if(data != null) // instead of: if(outputFileUri != null){
But this will never enter the "else" part of the code.
use these conditions:
private static final int CAMERA_PIC_REQUEST = 1337;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_PIC_REQUEST && resultCode == RESULT_OK){
log.d("something","something");
}
else if (resultCode == Activity.RESULT_CANCELED)
{
log.d("something","something");
}
}
Related
The case is when user selects camera, he has the flexibility to either capture image or record video and the user shall be able to show the same in one's app.
For this case, MediaStore.INTENT_ACTION_VIDEO_CAMERA intent is used but no callback comes in either case of capturing image or recording video.
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);
For getting result, code used as follows:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri = data.getData();
}
}
}
To start the camera and get the result back in onActivityResult(), you should create an Intent with MediaStore.ACTION_IMAGE_CAPTURE:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Similarly, to capture a video, use MediaStore.ACTION_VIDEO_CAPTURE
I finally found a solution and it is as follows:-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);
And finally receives the callback in onActivityResult and I got the uri in this way:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri;
if (data == null || data.getData() == null) {
Bitmap bitmap (Bitmap)data.getExtras().get("data");
// TODO:Get uri from bitmap for image
uri = getImageUri(context, bitmap);
} else {
//Get uri for video
uri = data.getData();
}
}
}
}
I want to upload multiple images through android application to firebase. I want to get the URL of the image which i got in my second activity(ie, B Activity) to my first activityIA activity). I have tried many answers posted but I could not solve the issue. Can anyone help me please. Here is my code
B Activity
mSelectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE);
} });
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
totalItemsSelected = data.getClipData().getItemCount();
for(int i = 0; i < totalItemsSelected; i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
String fileName = getFileName(fileUri);
fileNameList.add(fileName);
fileDoneList.add("uploading");
uploadListAdapter.notifyDataSetChanged();
StorageReference fileToUpload = mStorage.child("Images").child(fileName);
final int finalI = i;
fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileDoneList.remove(finalI);
fileDoneList.add(finalI, "done");
uploadListAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
imageURL=imageUploadInfo.getImageURL();
imagesList.add(imageURL);
Intent idata = new Intent()
idata.putExtra("imageURL", imageURL);
idata.putExtra("count",totalItemsSelected);
setResult(RESULT_OK, idata);
finish();
}
});
}
A activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
if (requestCode == 123) {
if (resultCode == RESULT_OK) {
imageURL = data.getStringExtra("imageURL");
this.orderItem.setImageURL(imageURL);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
break;
case 2://added24
if(requestCode==100){//added24
if (resultCode == RESULT_OK) {
imageURL = data.getStringExtra("imageURL");
newString=data.getStringExtra("count");
}
}
break;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.order_item_update, menu);
FrameLayout image_viewCount = (FrameLayout) menu.findItem(R.id.star).getActionView();
TextView image_count = (TextView) image_viewCount.findViewById(R.id.cart_badge);
image_count.setText(newString);
image_viewCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),MultipleActivity.class);
Order.getInstance().getOrderItems();
startActivityForResult(intent,100);
}
});
I want to set the count (ie, "total selected" in image_count) but I am not able to get the value as I am not getting the value from Bactivity to A activity
change the onActivityResult in your A activity to,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 123:
if (resultCode == RESULT_OK) {
imageURL = data.getStringExtra("imageURL");
this.orderItem.setImageURL(imageURL);
} else if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
break;
case 100:
if (resultCode == RESULT_OK) {
imageURL = data.getStringExtra("imageURL");
newString = data.getStringExtra("count");
} else {
//Write your code if there's no result
}
break;
}
}
You were switching resultCode and the cases you used were 1 and 2. The int constant for RESULT_OK is -1.
And in your MultipleActivity activity, the condition data.getClipData() != null will only be true when there are multiple Uris to send back. When there is just one you can get it with getData. You can do it like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
if (data.getClipData() != null) {
totalItemsSelected = data.getClipData().getItemCount();
for (int i = 0; i < totalItemsSelected; i++) {
Uri fileUri = data.getClipData().getItemAt(i).getUri();
String fileName = getFileName(fileUri);
fileNameList.add(fileName);
fileDoneList.add("uploading");
uploadListAdapter.notifyDataSetChanged();
StorageReference fileToUpload = mStorage.child("Images").child(fileName);
final int finalI = i;
fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileDoneList.remove(finalI);
fileDoneList.add(finalI, "done");
uploadListAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
imageURL = imageUploadInfo.getImageURL();
imagesList.add(imageURL);
Intent idata = new Intent();
idata.putExtra("imageURL", imageURL);
idata.putExtra("count", totalItemsSelected);
setResult(RESULT_OK, idata);
finish();
}
});
}
} else {
Uri fileUri = data.getData();
String fileName = getFileName(fileUri);
fileNameList.add(fileName);
fileDoneList.add("uploading");
uploadListAdapter.notifyDataSetChanged();
StorageReference fileToUpload = mStorage.child("Images").child(fileName);
fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileDoneList.remove(0);
fileDoneList.add(0, "done");
uploadListAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
imageURL = imageUploadInfo.getImageURL();
imagesList.add(imageURL);
Intent idata = new Intent();
idata.putExtra("imageURL", imageURL);
idata.putExtra("count", 1);
setResult(RESULT_OK, idata);
finish();
}
});
}
}
}
Your issue is that the fileToUpload is async and you are creating them in a loop; you close the current activity when one of those tasks ends (the first one). You can simulate a semaphore, to mark the completion of each fileToUpload tasks and store the result in it, and in the onSuccess method, just check if all task instances are completed before closing the current activity and passing the result (which should be an array of objects btw).
As I understood:
Activity-A opens Activity-B using startActivityForResult.
and you want image url which you got in Activity-B to Activity-A via onActivityResult().
If so then,
you have to add your activity finish code in some action not into onActivityResult() of Activity-B.
Because adding it to onActivityResult() means you are waiting for the response of any Activity you open from Activity-B using startActivityForResult.
I've got a code that starts intent to take picture from camera
Intent pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent, PhotoUtility.CAMERA_PICTURE);
And after picture is taking back it should calls onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == PhotoUtility.CAMERA_PICTURE) {
File f = PhotoUtility.saveFromCamera(data, picIV);
imagePath = f.getAbsolutePath();
} else if (requestCode == PhotoUtility.GALLERY_PICTURE) {
File f = PhotoUtility.saveFromGallery(data, getActivity(), picIV);
imagePath = f.getAbsolutePath();
}
}
With simple Fragment it works fine, but if I use DialogFragment onActivityResult is not calling. How can solve this problem using DialogFragment?
I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}
This is my current code:
public void onClick(View v) {
final Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
final Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
final Intent camIntent = new Intent("android.media.action.IMAGE_CAPTURE");
pickIntent.putExtra(Intent.EXTRA_INTENT, camIntent);
pickIntent.putExtra(Intent.EXTRA_INTENT, gallIntent);
pickIntent.putExtra(Intent.EXTRA_TITLE, "Select Source");
startActivityForResult(pickIntent, 0);
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(CreatePod.this, "Uploading",
"Please wait...", true);
//new ImageUploadTask().execute();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast toast = Toast.makeText(this,"camera cancelled", 10000);
toast.show();
return;
}
// lets check if we are really dealing with a picture
if (requestCode == 0 && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
//setContentView(R.layout.main);
imgView.setImageBitmap(b);
// save image to gallery
String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
}
}
This gives the options Gallery and Camera(Actually, it shows an unsupported device in the place of camera. If I click on it, it gives NullPointerException and crashes. Is this the right way to do it? Or should I use PackageManger? If Yes, then how?
Looking of your code, I think you have a lack of basic android knowledge,
Miss-use of onActivityResult(), its a method of Activity class, take out it from onClick()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
}
Just check basic android activity's method and how to use them.
Update:
Look at Android-Activity