How to show progress bar while sending mutiple image file to email - android

I have an Intent class to send multiple images to the email as an attached file. So all works fine. Only issue is that when no of images is more than 20 then it The Intent class takes time to open a particular mail client. So during that time i want to show progress bar to the user. So anyone help me to solve this out. My code for sending multiple images is shown below.
ArrayList<Uri> uris = new ArrayList<Uri>();
for(int i = 0; i< NoteManager.getSingletonObject().getNoteItemCount(); i++)
{
File imageFile = new File(m_ShareDir, NoteManager.getSingletonObject().getNote(i));
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.DATA, imageFile.getAbsolutePath());
Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
uris.add(imageUri);
}
Intent intent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/png");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));

May it helps you
ProgressDialog pd = ProgressDialog.show(SetFrames.this,"Loading", "Please
Wait...",true);
new Thread() {
public void run() {
try {
//do your process
}catch(Exception e)
{
}
handler.sendEmptyMessage(0);
pd.dismiss();
}
}.start();
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
}
};

Try this. tutorial. And I think here is good example for you which show the progress item one by one.
Enjoy...

Related

Is it possible to send video url to instagram application using intent in android?

I am using below code for sending video url to instagram app but no luck
String type = "video/*";
String mediaPath= "www.example.com/abcd.mp4";
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
After lot of searching finally I found the solution. First we need to download video from url(if video not stored in local storage) then we apply below code.
/* Button click to share video on Instagram */
btn_instagram.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isPackageInstalled("com.instagram.android")) {
createInstagramIntent(localuri);
}else{
AlertDialog.Builder alert = new AlertDialog.Builder(ReflipActivity.this);
alert.setTitle("Warning");
alert.setMessage("Instagram App not found");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
}
});
// Check Instagram app is installed on device or not
private boolean isPackageInstalled(String packagename) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
// And finally fire the Intent for Instagram
private void createInstagramIntent(String filename){
String settype = "video/*";
String mediaPath = filename;
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(settype);
share.setPackage("com.instagram.android");
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}

Share image and text through Whatsapp or Facebook

I have in my app a share button and i want to share an image and a text at the same time. In GMail it works fine but in WhatsApp, only the image is sent and in Facebook the app crashes.
The code i use to share is this:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Message");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/drawable/ford_focus_2014");
try {
InputStream stream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
If I use "shareIntent.setType("*/ *")" Facebook and WhatsApp crashes.
Is there some way to do this? Maybe sent two messages by separate at the same time (WhatsApp).
Thanks in advance.
Currently Whatsapp supports Image and Text sharing at the same time. (Nov 2014).
Here is an example of how to do this:
/**
* Show share dialog BOTH image and text
*/
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Please try the below code and hopefully it will work.
Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
For sharing text and image on WhatsApp, more controlled version of code is below, you can add more methods for sharing with Twitter, Facebook ...
public class IntentShareHelper {
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
For getting Uri from File, use below class:
public class UtilityFile {
public static #Nullable Uri getUriFromFile(Context context, #Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents
As of now, a Whatsapp Intent supports image and text:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image via:"));
The image will be as it is and EXTRA_TEXT will be shown as the caption.
try with this code
Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Kindly install whatsapp first");
}
public void shareIntentSpecificApps(String articleName, String articleContent, String imageURL) {
List<Intent> intentShareList = new ArrayList<Intent>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//shareIntent.setType("image/*");
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo resInfo : resolveInfoList) {
String packageName = resInfo.activityInfo.packageName;
String name = resInfo.activityInfo.name;
Log.d("System Out", "Package Name : " + packageName);
Log.d("System Out", "Name : " + name);
if (packageName.contains("com.facebook") ||
packageName.contains("com.whatsapp")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, articleName);
intent.putExtra(Intent.EXTRA_TEXT, articleName + "\n" + articleContent);
Drawable dr = ivArticleImage.getDrawable();
Bitmap bmp = ((GlideBitmapDrawable) dr.getCurrent()).getBitmap();
intent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bmp));
intent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareList.add(intent);
}
}
if (intentShareList.isEmpty()) {
Toast.makeText(ArticleDetailsActivity.this, "No apps to share !", Toast.LENGTH_SHORT).show();
} else {
Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Articles");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
You can share image also I have done in my app like mentioned in above code.
My second answer for this question is: I'm pasting full code here because new developer need sometimes full code.
public class ImageSharer extends AppCompatActivity {
private ImageView imgView;
private Button shareBtn;
FirebaseStorage fs;
StorageReference sr,sr1;
String Img_name;
File dir1;
Uri uri1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_sharer);
imgView = (ImageView) findViewById(R.id.imgView);
shareBtn = (Button) findViewById(R.id.shareBtn);
// Initilize firebasestorage instance
fs=FirebaseStorage.getInstance();
sr=fs.getReference();
Img_name="10.jpg";
sr1=sr.child("shiva/"+Img_name);
final String Paths= Environment.getExternalStorageDirectory()+ File.separator+"The_Bhakti"+File.separator+"Data";
dir1=new File(Paths);
if(!dir1.isDirectory())
{
dir1.mkdirs();
}
sr1.getFile(new File(dir1,Img_name)).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
sr1.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
uri1= Uri.parse(uri.toString());
}
});
}
}) ;
shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
uri1=Uri.parse(Paths+File.separator+Img_name);
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//intent.putExtra(intent.EXTRA_SUBJECT,"Insert Something new");
String data = "Hello";
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT,data);
intent.putExtra(Intent.EXTRA_STREAM,uri1);
intent.setPackage("com.whatsapp");
// for particular choose we will set getPackage()
/*startActivity(intent.createChooser(intent,"Share Via"));*/// this code use for universal sharing
startActivity(intent);
// end Share code
}
});
}// onCreate closer
}
Use this code for sharing on whatsapp or on another package with image and video. Here the URI is the path of image. If image in Memory then it took fast loading and if you are using url then sometimes images don't load and links gone direct.
shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
uri1=Uri.parse(Paths+File.separator+Img_name);
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//intent.putExtra(intent.EXTRA_SUBJECT,"Insert Something new");
String data = "Hello";
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT,data);
intent.putExtra(Intent.EXTRA_STREAM,uri1);
intent.setPackage("com.whatsapp");
startActivity(intent);
// end Share code
}
If this code is not understandable then see the full code in my other answer.
Actually. it is possible to send image and text through WhatsApp by downloading the image to device external storage and then share the image to WhatsApp.
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Intent intent = new Intent(Intent.ACTION_SEND);
String share_text = "image and text";
intent.putExtra(Intent.EXTRA_TEXT, notification_share);
String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bm, "", null);
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
//The above code works perfect need to show image in an imageView
This works:
<activity android:name="com.selcuksoydan.sorucevap.Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
soru_image = (ImageView) soruView.findViewById(R.id.soru_image);
soru_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
v.buildDrawingCache();
Bitmap bitmap = v.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/SoruCevap");
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception ex) {
//ignore
}
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
waIntent.setType("image/*");
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file));
getContext().startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (Exception e) {
Log.e("Error on sharing", e + " ");
Toast.makeText(getContext(), "App not Installed", Toast.LENGTH_SHORT).show();
}
This worked for me in January 2019
private void shareIntent() {
Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
String imgBitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imgBitmapUri = Uri.parse(imgBitmapPath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_TEXT, "My Custom Text ");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
startActivity(Intent.createChooser(shareIntent, "Share this"));
}
This will let the user share the image + text to WhatsApp and all other apps the user wants, it's always the best to let the user select where to share the content instead of prompting just WhatsApp.
Also make sure that if you include just WhatsApp to share it might not be installed in some devices, for this you will need a try catch and inside of it the startActivity(intent); and also set the package of the intent to just WhatsApp with intent.setPackage("com.whatsapp").
Copy text from anywhere.let it be Google, Facebook or whatsapo itself
attempt to upload the image in whatsapp anywhere.at contact or group.before you hit the send image arrow... you will see caption option to that image... touch and hold, a paste option will appear.hit paste... your text will show up... then you can send the photo.and it will appear with the text you wanted... there you go... you have the text and image in it... the only problem will be the text size, which is limited to certain number of words
😀 This works for Android users only

android: download a pdf file and open after

I'm trying to download a pdf doc from internet and after that, when download ends, open it automatically throught a package.
I found some solutions for downloading by DownloadManager but no answer to open it after. One of the codes I've tested is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri uri = Uri.parse(uriString);
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.xxxxxxx.org/directory/abc.pdf"));
enqueue = dm.enqueue(request);
}
but when the download ends successfully, the package don't find the file.
I need your help. I have spent two weeks looking for a solutions via google and android books with no success.
Thanks.
Create this method and call it in your code
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
Intent testIntent = new Intent("com.adobe.reader");
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
Or else you can refer my answer here.
How to use Asynchronous task for download progress dialog in fragment?
Its for downloading file and displaying progress diolog.Here i used asynch task.Call above method in onpost() method of asynch task.
Hope it helps you too

How to show a progress dialog while waiting for share intent to pop up

I noticed on older devices that once you do a standard share intent (fb, twitter, email, etc) there is a few second delay until the actual share dialog is popped up. I was wondering how you would be able to have a progress dialog pop up while you are waiting for this share dialog to show up?
Here is my code:
private void share(String subject,String body) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
share.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(share, "Share via"));
}
I figured out how to do it. When you call startActivity(shareIntent) the onPause in your method gets called. So to solve this I did the following:
Create a class progress dialog variable.
Show the progress dialog at the start of my share function.
In the onPause i did the following:
if(dialog.isShowing()){
dialog.dismiss();
}
This doesn't seem to be possible - you don't have any way of knowing when the Intent Chooser is ready to display. As far as your app is concerned, this appears to be an asynchronous operation.
To confirm this, you could try timing the function:
private void share(String subject,String body) {
//Timing code
long startTime = System.currentTimeMillis();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
share.putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(share, "Share via"));
//Timing code
long endTime = System.currentTimeMillis();
Log.d("Test", "Time for share function: " + (endTime-startTime) + "ms");
}
If you get a very small result, it means your function is executing quickly, and the bottleneck is in the Android system itself (which you can't affect).
If, on the other hand, you get something in the order of 500+ milliseconds, it could be worth using the method described by XepterX and display a Progress Dialog.
I would suggest below method:
First create a progress dialog and show it
Call Share Activity
Hide progress dialog returning back to main activity (onResume)
Here is the code:
ProgressDialog shareWaitDialog;
private void shareIt() {
String subject="Subject";
String text = "text....";
shareWaitDialog = new ProgressDialog(getActivity());
shareWaitDialog.setTitle("Sharing...");
shareWaitDialog.setMessage(subject);
shareWaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
shareWaitDialog.show();
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
text));
startActivity(Intent.createChooser(sharingIntent,
"Share using:"));
}
#Override
public void onResume() {
if (shareWaitDialog != null && shareWaitDialog.isShowing())
shareWaitDialog.hide();
super.onResume();
}
It seems very difficult to show dialog, instead You Can Show Toast like "Waiting for Apps..." on Share Button Click. The Toast can fill the time gap.
mShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ReferralActivity.this, "Waiting for Apps to Send", Toast.LENGTH_SHORT).show();
try {
String sAux = "Content To Share";
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sample");
i.putExtra(Intent.EXTRA_TEXT,sAux);
startActivity(Intent.createChooser(i, "Share using:"));
} catch(Exception e) {
//e.toString();
}
}
});
You Can try this code:
void share(String link) {
final ProgressDialog ringProgressDialog = ProgressDialog.show(this, "", getString(R.string.please_wait), true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
public void run() {
try {
// your share method for sharing link or image or anything
shareTextUrl(link);
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
ringProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
}
});
}
You could use an AsyncTask() to help you show a dialog progress, for example,
private class TestTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
//show dialog
}
protected void doInBackground(Object... params) {
private void share(subject,body);
}
protected void onPostExecute(Long result) {
//cancel dialog
}
}
and start the task by
new TestTask().execute();
You can find more info regarding AsyncTask() from the documentation here.

Launch SMS using data from a scanned QR Code

I'm trying to start the SMS activity with the phone number from a scanned QR Code filled in. So far I've been able to get the activity to launch but the number and message fields are blank. Here's what I have, currently this is crashing the app:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
Uri uri = Uri.parse(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setType("vnd.android-dir/mms-sms");
i.setData(uri);
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
The String smsUri contains the scanned string from the QR Code, "SMSTO:666-666-1234:hello". How can I get the SMS Activity to launch with the phone number and the message already entered into the number and body fields?
I saw this post:
Sending SMS using Intent does not add recipients on some devices
Would I need to parse the QR Code result myself and break it into the phone number and message, then add those as Extras like that example?
Got it!
Since I can't answer my own question yet here it is:
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}
Okay, got it to work. I made a class to break the QR result into separate elements:
public class Sms {
public static String[] breakString(String s) {
String[] smsElements = s.split(":");
return smsElements;
}
}
Then changed the method I have above to:
else if(resultType.getParsedResultType() == ParsedResultType.SMS){
Button smsButton = (Button)findViewById(R.id.btn_send_sms);
smsButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String smsUri = ResultsActivity.this.item.getContent();
String[] smsElements = Sms.breakString(smsUri);
Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", smsElements[1]);
i.putExtra("sms_body", smsElements[2]);
i.setData(Uri.parse("smsto:" + smsElements[1]));
startActivity(i);
}
});
smsButton.setVisibility(View.VISIBLE);
}

Categories

Resources