I am developing an application in which I have to send an image through email. I succeeded in sending email, but the attachment sent is of 0KB size. I am not getting what is the problem. Below, I am posting my code.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class EtestActivity extends Activity {
/** Called when the activity is first created. */
Button email;
Intent in;
private static final String TAG = "EmailLauncherActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
email = (Button)findViewById(R.id.email);
email.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
in = new Intent(Intent.ACTION_SEND);
in.setType("image/jpeg");
in.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/android.jpg"));
startActivity(Intent.createChooser(in, "Email..."));
} catch (Exception e) {
Log.e(TAG, "email sending failed", e);
}//catch
}//onClick
});
}//onCreate
}//class
I also face the same problem which is solved by changing MIME type.
Try this in.setType("image/jpg");
Was your phone mounted to your computer at the time? The SD card may be inaccessible while connected to your computer, so it may not actually be attaching the image.
Related
Extremely new to coding(yesterday)
I have been given the task of creating a Android app for a friend to pull the data from his website and insert it into a quick app rather than him and his colleagues having to open a browser every time they would like to view it.
For reasons, I cannot share the website address.
The data on the website is in a bullet point list.
I have been reading some tutorials and watching some videos, I can pull the website up using WebView and I have now started reading up on JSoup. I have copied a few lines of code from some tutorials and I can now pull the website up in raw/html/just plain text but I could like to remove the unnecessary data and just have the text next to the bullet points.
Here is my code from MainActivity.java
As previously mentioned, I am extremely new to this with no background experience.
What I would like to know is if I can scrape the text between two tags on the page source for example between "div class="scheduler" SCRAPE THE TEXT HERE "ul id="myUL"
That would have all of the information I need in the above tags.
Thank you.
package spcentral.jsouptut;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button getBtn;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
getBtn = (Button) findViewById(R.id.getBtn);
getBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getWebsite();
}
});
}
private void getWebsite() {
new Thread(new Runnable() {
#Override
public void run() {
final StringBuilder builder = new StringBuilder();
try {
Document doc = Jsoup.connect("linkremoved").get();
String text = doc.text();
Elements links = doc.select("a[href]");
builder.append(text).append("n");
for (Element link : links) {
builder.append("n").append("Link : ").append(link.attr("href"))
.append("n").append("Text : ").append(link.text());
}
} catch (IOException e) {
builder.append("Error : ").append(e.getMessage()).append("n");
}
runOnUiThread(new Runnable() {
#Override
public void run() {
result.setText(builder.toString());
}
});
}
}).start();
}
Hi How to get scanned image is barcode or QR? Here is my code where I can get only product id but i couldn't get scanned format name. How to solve this. Please me to solve this.
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import com.contus.sportscorner.utils.Constants;
import com.google.zxing.Result;
import com.google.zxing.client.android.CaptureActivity;
public class ScannerActivity extends CaptureActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barcode_scan);
}
#Override
public void handleDecode(Result rawResult, Bitmap barcode) {
String productId = rawResult.getText();
Intent intent = new Intent(ScannerActivity.this, NextAcitivity.class);
intent.putExtra(Constants.ID, productId); // i am getting just product
// id
startActivity(intent);
finish();
}
}
Use this.
String barcodeType = rawResult.getBarcodeFormat().toString();
First you should be converted into string then compare these values
how to convert in to string
I have a problem with the concatenation of the strings that I will I enter.
This is the class:
package com.isma.multisitesearch.Siti;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Webviews.GoogleWebView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Google extends Activity implements OnClickListener {
private String TAG_ACT = "Caricamento ricerca";
public EditText googletext;
private Button googlebutton;
private String googleurl = "https://www.google.it/search?q=";
public static String newgoogleurl;
public String space = " ";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
googletext = (EditText) findViewById(R.id.googletext);
googlebutton = (Button) findViewById(R.id.googlebutton);
googlebutton.setOnClickListener(this);
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl.replaceAll(space, "%20");
System.out.println(newgoogleurl);
}
#Override
public void onClick(View v) {
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
}
And this is the WebView to which I want to get the concatenation:
package com.isma.multisitesearch.Webviews;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Siti.Google;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class GoogleWebView extends Activity {
private WebView googlewebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlewebview);
googlewebview = (WebView) findViewById(R.id.googlewebview);
googlewebview.getSettings().setJavaScriptEnabled(true);
googlewebview.getSettings().setLoadsImagesAutomatically(true);
googlewebview.setWebViewClient(new WebViewClient());
googlewebview.loadUrl(Google.newgoogleurl);
}
}
The app works fine but when I go to write the search text in the EditText, the google but I get the main page where you can enter the search instead of the url with already entered the search text.
I hope you know to help me, after this I was able to run the app.
Try this:
#Override
public void onClick(View v) {
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
Note that
newgoogleurl.replaceAll(space, "%20");
has no effect, you need to code:
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Have a look here:
http://javarevisited.blogspot.it/2011/12/java-string-replace-example-tutorial.html
Try below code:
String url;
url = googleurl+googletext.getText().toString();
if( url.contains(" ")){
newgoogleurl=url.replaceAll(" ","%20");
}else{
newgoogleurl=url;
}
This is containing validation part also.
Strings are immutable. That means that you cannot modify an instance of String, and need to create a new one each time you want to modify it.
In your case, replaceAll does not modify the String you call it on, but rather returns a new String with your modification applied.
Which is why you need to affect the returned value to the reference of your String.
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Moreover, you should read the documentation for replaceAll, because I don't think it does what you want. You probably want to use replace.
In my example, i am entering mobile no., message content in one activity. Before leaving that activity i am saving that info in "Shared Preference". In other activity i am trying to get those mob no,message,i am able to get no but unable to getting that message(second value).please help me to solve the issue.
DefaultDetails.java
package com.example.nirbhaya;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DefaultDetails extends Activity implements OnClickListener{
Button save,reset;
EditText dMob,dMsg,dEmail;
String defMobNo,defMsg,defEmail;
SharedPreferences DefaultData;
private static final String TAG = "DD-Activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.defaultdetails);
initializing();
}
private void initializing() {
// TODO Auto-generated method stub
save = (Button)findViewById(R.id.bsave1);
reset = (Button)findViewById(R.id.bReset);
dMob = (EditText)findViewById(R.id.etDefMobNo);
dMsg = (EditText)findViewById(R.id.etDefMsg);
dEmail = (EditText)findViewById(R.id.etDefEmail);
save.setOnClickListener(this);
reset.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.bsave1:
defMobNo = dMob.getText().toString();
defMsg = dMsg.getText().toString();
defEmail = dEmail.getText().toString();
Log.i(TAG,"DONE");
DefaultData = getSharedPreferences("defMobileNo",0);
SharedPreferences.Editor store = DefaultData.edit();
store.putString("defMobileNo", defMobNo);
store.putString("defMessgae", defMsg);
store.putString("defEMail", defEmail);
store.commit();
Intent openStartingPoint = new Intent (getApplicationContext(), CurrentDetails.class);
startActivity(openStartingPoint);
break;
case R.id.bReset:
((EditText) findViewById(R.id.etDefMobNo)).setText("");
((EditText) findViewById(R.id.etDefEmail)).setText("");
((EditText) findViewById(R.id.etDefMsg)).setText("");
break;
}
}
}
DefSMS.java
package com.example.nirbhaya;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class DefSms extends Activity{
Button buttonSend;
String defNo,defMsg;
SharedPreferences DefaultData;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.defsms);
DefaultData = getSharedPreferences("defMobileNo",0);
final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");
DefaultData = getSharedPreferences("defMessgae",0);
final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(defNo, null, defMsg, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}
Here def no getting message as Couldn't load data
please help me
Replace with this:
DefaultData = getSharedPreferences("defMobileNo",0);
final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");
final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");
This is how basically sharedpreference work
To Store the values
SharedPreferences preferences = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","myNameisnothing");
editor.commit();
To get the values
SharedPreferences prfs = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE);
String name = prfs.getString("Name", "");
This way it'd return the value of the name as myNameisnothing.
PS.Correct me if im wrong.
You are wrongly using
DefaultData = getSharedPreferences("defMessgae",0); in DefSMS.java
Please remove this and it will work fine.
You were trying to get shared preference named defMessgae which doesnot even exist. So when you try to access it, android will create a new preference with default value. That is why you were getting "Couldn't load data"
i seem to have encountered a problem here.. i have the 2 edittext boxes and one button. when i click the button it gives me an option of what way to send the message, however it does not capture what my inputs are but gives out a weird message saying This is a Testandroid.widget.EditText#47b84299android.widget.EditText#47b8f0d9. Neither does it go to email and fill up the subject header.
this is my code.
package com.emailmetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Activity1 extends Activity implements OnClickListener {
Button sendemail;
TextView input1, input2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendemail =(Button)findViewById(R.id.sendemail);
input1 = (TextView)findViewById(R.id.input1);
input2 = (TextView)findViewById(R.id.input2);
sendemail.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"test#hotmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "\nThis is a Test" + input1 + input2);
try {
startActivity(Intent.createChooser(i, "Send via..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this.getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
try:
i.putExtra(Intent.EXTRA_TEXT , "\nThis is a Test" + input1.getText().toString() + input2.getText().toString());