I'm making an app that can take photos. If the taken photo is saved into a folder I want a toaster message to appear.
However with my current code when I click the camera button, the toaster just comes up without waiting for the photo to be taken.
Below is the source code to take the photo:
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
public void onClickbtnCamera(View v){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Uri uriSavedImage=Uri.fromFile(
new File("/storage/emulated/0/DCIM/Camera","QR_"+timeStamp+ ".png"));
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, 1);
}
The toaster I want to show:
Toast toast= Toast.makeText(getApplicationContext(),
"Picture was taking", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
Can you tell me why my code is failing to do the intended job?
#Craig The question you've asked isn't clear/complete.
Assuming from the information you have provided, that you want a message to be displayed on a screen once a picture is saved. And you are using
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
Have an event listener on imageIntent object. Register that event to the method that contains this piece of code.
Toast toast= Toast.makeText(getApplicationContext(),
"Picture was taking", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
PS: This might not be the actual solution but hope should help you to give a direction
Related
So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).
To work towards this goal, I found this post on how to only show a toast if none are being shown.
I have modified the code to fit both requirements.
private Toast toast;
public void showAToast (String st, boolean isLong){
try{
toast.getView().isShown();
String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
if(!text.equalsIgnoreCase(st)){
//New message, show it after
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
} catch (Exception e) {
//New message
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
}
My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.
Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.
The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.
So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.
I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
In the OnCreate:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
//And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
// If you need to just cancel an existing toast (before it times out) use:
toast0.cancel();
toast1.cancel();
Quotation
You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.
Toast mToast;
public void showToast(CharSequence message, int during){
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), message, during);
} else {
mToast.setText(message);
}
mToast.show();
}
--↓---↓----↓---update--↓----↓---↓--↓
Sorry about that I miss your point above.
I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.
But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.
I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on WhatsApp.
The code is:
String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uriToImage= Uri.parse(imageUrl);
Log.d("Image", ""+uriToImage);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image:"));
}
});
What's happening is:
On WhatsApp I can share the image easily.
On Gmail, it says that the attachment could not be sent.
On Hangouts I get a toast which says Photo couldn't be found
On Facebook too, the post is not accompanied with the Image but I can post.
On Facebook Messenger, it crashes without opening.
The tutorial that I have followed for this is given here. The send binary content part of the tutorial is the one that I have implemented.
Another thing I tried was to set the image in an ImageView and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.
I also read and tried the answers to: Question 1 and Question 2 but to no avail.
Where am I going wrong?
try this,
try
{
File myFile = new File(share_image_path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Yes the #CommonsWare Answer is correct you missed the Schema in your Intent.putExtra() and that's why it is failing to read your image in other social media platform
Here's my solution
Uri fileUri = Uri.fromFile(new File(imagePath));
//No need to do mimeType work or ext
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image:"));
BTW it works on all the mentioned platform
Uri image_uri = Uri.parse("file://"+imagePath);
Another method of creating Uri and passing it to intent
How would I be able to call a string from one activity to another?
My code isn't working, I am trying to achieve a program that can browse
the images on the SD card of my phone and return the address of the image.
I am developing a QR Code decoder based on the zxing library.
private void onFileClick(Option o)
{
//Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "File Clicked: "+o.getPath(), Toast.LENGTH_SHORT).show();
QRDecoder qr = null;
str = o.getPath();
qr.setFile(str);
Intent intent = new Intent(this, QRDecoder.class);
Log.d("filter", str);
Log.d("filter", qr.my_url);
startActivity(intent);
}
before calling startActivity
intent.putExtra("path", str);
in onCreate of the QRDecoder
String path = getIntent().getStringExtra("path");
I have been trying to get notifications to launch on Android at the correct time. I want them to go off 2 mins in the future from App launch. If I can get it working here I can easily get it working where I really need to do it. The logs show that the needed lines of code are running but the notification never launches. I am up to over 8 hours of trying to debug this and getting no where. Any help would be great.
Here is the Reminder code I have created:
final PendingIntent pi = PendingIntent.getBroadcast(this.mContext,
0,
i,
PendingIntent.FLAG_ONE_SHOT);
TimeZone timeZone= TimeZone.getDefault();
Calendar time= Calendar.getInstance(timeZone);
time.add(Calendar.MINUTE, 2);
this.mAlarmManager.set(AlarmManager.RTC_WAKEUP,
time.getTimeInMillis(),
pi);
Log.d ("setReminder",time.getTime().toLocaleString());
Now below is my OnAlarmRecieve code that is never running 2 mins later according to the logs. The class extends BroadcastReceiver
#Override
public void onReceive(final Context context,
final Intent intent) {
Log.d(TAG, "Recieved wake up cal from Alarm Manger");
final String tableName = intent.getStringExtra(IntentExtraStringStorage.TABLE_NAME);
final long rowID = intent.getLongExtra(IntentExtraStringStorage.ROW_ID,
-1);
final String titleString = intent.getStringExtra(IntentExtraStringStorage.NOTIFICATION_TITLE);
final String notificationString=intent.getStringExtra(IntentExtraStringStorage.NOTIFICATION_NOTE);
WakeUpReminderIntentService.acuireStaticLock(context);
/*if (tableName== Task.TABLE_NAME) {
launchTaskView(context, rowID);
}
else {
Log.e (TAG, "Did not Launch");
Toast.makeText(context, "Did not work right", Toast.LENGTH_SHORT).show();
}*/
Intent i = new Intent (context, ReminderService.class);
i.putExtra("taskID", rowID);
i.putExtras(intent.getExtras());
Log.d(TAG, "Launched task");
//TODO Added code to build the screen correctly
//FIXIT this need to be finished.
context.startService(i);
}]=
The problem was not putting the OnAlarmReciever in the mainfest as a receiver, and there was something not being put into the services as well.
I have an activity which gets called by 2 intents, one after a simple menu-selection and the other way by a intent after a deletion of an item in a database. However, I wanted to display in the called activity a little Toast, but only when it's opened through the intent of the deletion. I thought of following solution
public void intentCheck(){
Log.d("ShowActivity","intentCheck() called");
Bundle extras = getIntent().getExtras();
if (extras != null){
String check = extras.getString("AdvancedViewActivityCall");
if(check == "calling"){
Log.d("ShowActivity","delete-intent succeeded");
Toast success = new Toast(ShowActivity.this);
success.makeText(ShowActivity.this, "Deletion succeded", Toast.LENGTH_LONG);
}
}
but it doesn't work... somehow, no toast gets displayed.
edit:// i applied success.show(); now, but now i get a RunetimeException O.o ( http://pastebin.com/Th3NY5d0 )
edit: SOLUTION: Toast.makeText(context, text, duration).show(); //seems to be the "static way", which eclipse proposed
Have you tried if ("calling".equals(check)) instead of if(check == "calling") ?
EDIT:
try Toast.makeText(context, text, duration).show();
you have to call show method for toast until otherwise toast will not display.
success.show();