I'm trying to a link to the Facebook application. If it doesn't exist, it should go to Facebook in the browser.
The problem is, when I click on the button that goes there it gives me this error:
Unfortunately, A has stopped
I couldn't figure what to do with #SuppressWarnings("null") and uri = null.
Help.
package com.example.a;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button dadclink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
dadclink = (Button) findViewById(R.id.dadclink);
dadclink.setOnClickListener(new OnClickListener() {
#SuppressWarnings("null")
#Override
public void onClick(View arg0) {
String uri = null;
if (!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri1 = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(uri1));
startActivity(intent);
} else {
String natgeo = "natgeo";
String uri1 = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
startActivity(i);
}
}
});
}
}
Edited
package com.example.a;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button dadclink;
protected String uri;
protected String uri1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
dadclink = (Button) findViewById(R.id.dadclink);
dadclink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(uri));
startActivity(intent);
} else {
String natgeo = "natgeo";
String uri1 = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
startActivity(i);
}
}
});
}
}
Edited again
public void addListenerOnButton() {
dadclink = (Button) findViewById(R.id.dadclink);
dadclink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String uri = "";
if (!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri1 = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
startActivity(intent);
} else {
String natgeo = "natgeo";
String uri1 = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
startActivity(i);
}
}
});
It seems as though you aren't really paying attention to what you are doing. You currently have this:
dadclink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
} else {
String natgeo = "natgeo";
String uri1 = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
startActivity(i);
}
}
});
But you have set your uri string as
protected String uri;
This means that the first time you click, since you haven't set your uri anywhere, you will hit this line
if(!uri.contains("https://www.facebook.com/")){
and you will be trying to do null.contains("https://www.facebook.com/");. But obviously null doesn't support any sort of function calls, in other words null.contains() will cause your app to crash with a NPE.
EDIT
Just because you are getting close but need some help wrapping up, this should be your onClick method
#Override
public void onClick(View arg0) {
String uri = "https://www.facebook.com/natgeo";
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
It is as simple as that, you are way over-complicating things.
Related
How can you redirect to another activity once the application that was launched by createChooser such as show below is complete?
My attempt below ends up with the second intent being triggered before the createChooser launches. I noticed that when I press the back button on the newly launched activity is when the createChooser appears on the activity I wanted to launch it from.
I also tried to wrap the createChooser in startActivityForResult and then launch the second intent using onActivityResult but the result was the same
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Intent trackIntent = new Intent(InformContacts.this, TrackOffers.class);
startActivity(trackIntent);
Here's the entire code:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class InformContacts extends Activity {
private static String ITEM_NAME = "Item name";
private static String ITEM_PRICE = "Item price";
private static String ITEM_PIC_URI = "Item pic uri";
public static final int REQUEST_SEND_EMAIL = 0;
ArrayList<Contact> listContacts;
ListView lvContacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inform_contacts);
listContacts = new ContactFetcher(this).fetchAll();
lvContacts = (ListView) findViewById(R.id.lvContacts);
final ContactsAdapter adapterContacts = new ContactsAdapter(this, listContacts);
lvContacts.setAdapter(adapterContacts);
final Button informButton = (Button) findViewById(R.id.button6);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final String uriSharedPref = preferences.getString(ITEM_PIC_URI, "item pic uri");
informButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList emailsList = adapterContacts.emailsSelected;
String item = preferences.getString(ITEM_NAME, "item name");
Float price = preferences.getFloat(ITEM_PRICE,0);
//May use the uriSharedPref string to embed actual url in email and show recipients like I did with publishing house
String sellingMessage = "Hello,\n\nI'm selling my "+ item +" for KES "+price+".\n\nGet back to me if you're interested in buying.\n\n" + uriSharedPref;
String subject = "Selling my "+item;
sendEmail(emailsList, sellingMessage, subject);
//Intent trackIntent = new Intent(InformContacts.this, TrackOffers.class);
//startActivity(trackIntent);
}
});
}
protected void sendEmail(ArrayList<String> arrayOfEmails, String message, String subject) {
Log.i("Send email", "");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
//First Step: convert ArrayList to an Object array
Object[] objEmails = arrayOfEmails.toArray();
//Second Step: convert Object array to String array
String[] strEmails = Arrays.copyOf(objEmails, objEmails.length, String[].class);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, strEmails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
if(isIntentSafe){
startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), REQUEST_SEND_EMAIL);
//finish();
Log.i("Done sending email...", "");
}
else{
Toast.makeText(InformContacts.this, "No email app found. Email won't be sent.", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if(requestCode == REQUEST_SEND_EMAIL){
Intent intent = new Intent(this, TrackOffers.class);
startActivity(intent);
}
}
}
}
Try this
Change to:
if(requestCode == REQUEST_SEND_EMAIL){
Intent intent = new Intent(this, TrackOffers.class);
startActivity(intent);
}
Instead of:
if (resultCode == RESULT_OK){
if(requestCode == REQUEST_SEND_EMAIL){
Intent intent = new Intent(this, TrackOffers.class);
startActivity(intent);
}
}
I want to send images via email in my android app. For which I'm using Android Native Camera app and Intents to use the respective service. I've used the following code:
Email is getting send but if I'm trying to add image the app gets crash.
public class Complaints extends AppCompatActivity {
Button sendEmail;
EditText to, subject, msg;
Bitmap image;
Button camera;
File pic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints);
to = (EditText) findViewById(R.id.et1);
subject = (EditText) findViewById(R.id.et2);
msg = (EditText) findViewById(R.id.et3);
sendEmail = (Button) findViewById(R.id.s_Email);
camera = (Button) findViewById(R.id.btn_img);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent,"Select Picture"));
}
});
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Emailid = to.getText().toString();
String sub = subject.getText().toString();
String message = msg.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{Emailid});
email.putExtra(Intent.EXTRA_SUBJECT, sub);
email.putExtra(Intent.EXTRA_TEXT, message);
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//this will make such that when user returns to your app, your app is displayed, instead of the email app.
email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
email.setType("message/rfc822");
email.setType("image/jpeg");
try {
startActivity(Intent.createChooser(email, "Message was Sent"));
}
catch (ActivityNotFoundException e) {
Toast t = Toast.makeText(Complaints.this, "There is No Emial Client installed ", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 10);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
image = (Bitmap) data.getExtras().get("Data");
ImageView i = (ImageView) findViewById(R.id.img);
i.setImageBitmap(image);
try
{
File root= Environment.getExternalStorageDirectory();
if(root.canWrite())
{
pic=new File(root,"pic.jpeg");
FileOutputStream out=new FileOutputStream(pic);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}
} catch (IOException e)
{
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
}
}
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class Complaints extends Activity {
Button send;
Bitmap thumbnail;
File pic;
EditText address, subject, emailtext;
protected static final int CAMERA_PIC_REQUEST = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints);
send=(Button) findViewById(R.id.emailsendbutton);
address=(EditText) findViewById(R.id.emailaddress);
subject=(EditText) findViewById(R.id.emailsubject);
emailtext=(EditText) findViewById(R.id.emailtext);
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"dummy#email.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"dummy subject");
//Log.d("URI#!##!#!###!", Uri.fromFile(pic).toString() + " " + pic.exists());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share this"));
}
});
}
Getting The Image Path and converting path into Uri :
File photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png")
Uri imageuri = Uri.fromFile(photo);
Sending through Email Intent :
Intent send_report = new Intent(Intent.ACTION_SEND);
send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid});
send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
send_report.putExtra(Intent.EXTRA_TEXT, email_body);
send_report.setType("text/plain");
send_report.setType("image/png");
startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);
I have a function which display FileDialog and Intent to use Bluetooth.
But when I press back button, it comes to previous activity, it is visible but not clickable (like screenshot) and I have to press the back button once again.
I tried function onBackPressed() { finish(); }, but nothing worked properly.
MainActivity:
...
if(item == shareMenu) {
startActivity(new Intent(getBaseContext(), ShareViaBluetoothActivity.class));
}
...
ShareViaBluetoothActivity:
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.util.List;
public class ShareViaBluetoothActivity extends Activity {
private static final int DISCOVER_DURATION = 300;
private static final int REQUEST_BLU = 1;
private FileDialog fileDialog;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
private File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File mPath = new File(Environment.getExternalStorageDirectory(), "//DIR//");
fileDialog = new FileDialog(this, mPath);
fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
public void fileSelected(File file) {
Log.d(getClass().getName(), "selected file " + file.toString());
setFile(file);
sendViaBluetooth();
}
});
fileDialog.showDialog();
}
public void sendViaBluetooth() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported on this device!", Toast.LENGTH_LONG).show();
} else {
enableBluetooth();
}
}
public void enableBluetooth() {
Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);
startActivityForResult(discoveryIntent, REQUEST_BLU);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(file.toString())));
intent.setPackage("com.android.bluetooth");
PackageManager pm = getPackageManager();
List<ResolveInfo> appsList = pm.queryIntentActivities(intent, 0);
if(appsList.size() > 0) {
String packageName = null;
String className = null;
boolean found = false;
for(ResolveInfo info : appsList) {
packageName = info.activityInfo.packageName;
if(packageName.equals("com.android.bluetooth")) {
className = info.activityInfo.name;
found = true;
break;
}
}
if (!found) {
Toast.makeText(this, "Bluetooth havn't been found",
Toast.LENGTH_LONG).show();
} else {
intent.setClassName(packageName, className);
startActivity(intent);
}
}
} else {
Toast.makeText(this, "Bluetooth is cancelled", Toast.LENGTH_LONG)
.show();
}
}
}
What you described was not so complete. But I this there are some reasons may result to this, then you can check it.
1. use super.onBackPressed() or give onBackPressed() return can make different
2. please check activity launch mode work.
but you'd better give a more detailed code,then I can help you
My app is to open the built in camera and save it into sd card then start a new activity and preview the captured image on it.
This is the loginActivity that start a new AsynTask to open the camera :
package com.android.grad;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.Camera.R;
public class LoginActivity extends Activity {
private final int MEDIA_TYPE_IMAGE = 1;
private CheckBox rememberMe;
private EditText userName, passWord;
private Button loginBtn;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private String path;
private Uri fileUri;
private OnClickListener loginOnClick = new OnClickListener() {
public void onClick(View v) {
new LoginTask(LoginActivity.this, fileUri).execute();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
path = fileUri.getPath();
loginBtn = (Button) findViewById(R.id.signInBtn);
loginBtn.setOnClickListener(loginOnClick);
rememberMe = (CheckBox) findViewById(R.id.keepMe);
userName = (EditText) findViewById(R.id.userNameID);
passWord = (EditText) findViewById(R.id.passwordID);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
try {
userName.setText(sp.getString("username", ""));
passWord.setText(sp.getString("password", ""));
rememberMe.setChecked(sp.getBoolean("rememberMe", false));
} catch (ClassCastException ex) {
}
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
Editor editor = sp.edit();
if (rememberMe.isChecked()) {
editor.putString("username", userName.getText().toString());
editor.putString("password", passWord.getText().toString());
} else {
editor.putString("username", "");
editor.putString("password", "");
}
editor.putBoolean("rememberMe", rememberMe.isChecked());
editor.commit();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Intent intent = new Intent(LoginActivity.this,
PreviewActivity.class);
intent.putExtra("path", path);
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(getApplicationContext(),
"User cancelled the image capture", Toast.LENGTH_LONG)
.show();
} else {
// Image capture failed, advise user
}
}
}
/** Create a file Uri for saving an image or video */
private Uri getOutputMediaFileUri(int type) {
File f = getOutputMediaFile(type);
if (f == null) {
Toast.makeText(LoginActivity.this, "MyCameraApp File not found",
Toast.LENGTH_LONG);
return null;
}
return Uri.fromFile(f);
}
/** Create a File for saving an image or video */
private File getOutputMediaFile(int type) {
// Check if SD card is mounted
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File externalFilesDir = LoginActivity.this
.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File outFile = new File(externalFilesDir, "IDOCR");
if (!outFile.exists())
outFile.mkdirs();
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String path = outFile.getPath() + File.separator + "IMG"
+ timeStamp + ".jpg";
File mediaFile = new File(path);
return mediaFile;
}
Toast.makeText(LoginActivity.this, "SD card unmounted",
Toast.LENGTH_LONG);
return null;
}
}
And this is the AsynTask :-
package com.android.grad;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
public class LoginTask extends AsyncTask<Void, Void, Boolean> {
private Activity activity;
private ProgressDialog pd;
private Uri fileUri;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public LoginTask(Activity activity, Uri fileUri) {
this.activity = activity;
this.fileUri = fileUri;
}
#Override
protected void onPreExecute() {
pd = ProgressDialog.show(activity, "Signing in",
"Please wait while we are signing you in..");
}
#Override
protected Boolean doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return true;
}
#Override
protected void onPostExecute(Boolean result) {
pd.dismiss();
pd = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
activity.startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
and this is the previewActivity:-
package com.android.grad;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import com.Camera.R;
import com.OCR.ID.AndroidImage;
import com.OCR.ID.Segement;
public class PreviewActivity extends Activity {
private ImageView previewIV;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private String path;
private boolean crop = true;
private boolean resample = true;
private ProgressDialog pd;
OnClickListener processOnClickListener = new OnClickListener() {
public void onClick(View v) {
try {
createProgressDialog();
pd.show();
new Segement(PreviewActivity.this, pd).execute(path);
} catch (IOException e) {
}
}
};
private void createProgressDialog() {
pd = new ProgressDialog(PreviewActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setTitle("Extract ID");
pd.setMessage("Processing...");
pd.setIcon(R.drawable.ic_launcher);
pd.setProgress(0);
pd.setCancelable(false);
}
private OnClickListener backOnClickListener = new OnClickListener() {
public void onClick(View v) {
startActivityForResult(
new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(path))),
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
previewIV.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
path = getIntent().getExtras().getString("path");
previewIV = (ImageView) findViewById(R.id.previewPicID);
previewIV.setImageBitmap(AndroidImage.decodeSampledBitmapFromSDcard(
path, 150, 150));
// previewIV.setImageBitmap(AndroidImage.readImage(path));
Button process = (Button) findViewById(R.id.processID);
process.setOnClickListener(processOnClickListener);
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(backOnClickListener);
}
}
The error that appear that sometimes when take picture it appear in the previewActivity and
sometime doesn't appear ?
Why that error
??
I'am uing the below code to open the android default gallery app. It opens all the image folders under sdcard. How can i open only one particular folder?
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick any photo"), SELECT_IMAGE_FROM_GALLERY_CONSTANT);
Use this following code to get a particular folder image.
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SDCARD123Activity extends Activity implements MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE="image/*";
private MediaScannerConnection conn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File("/sdcard/Photo/");
allFiles = folder.list();
// uriAllFiles= new Uri[allFiles.length];
for(int i = 0; i < allFiles.length; i++) {
Log.d("all file path" + i, allFiles[i]+allFiles.length);
}
// Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/Photo/"+allFiles[0];
System.out.println(" SCAN_PATH " +SCAN_PATH);
Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
Button scanBtn = (Button)findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
startScan();
}});
}
private void startScan() {
Log.d("Connected","success"+conn);
if (conn!=null) {
conn.disconnect();
}
conn = new MediaScannerConnection(this,this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
Log.d("onMediaScannerConnected","success"+conn);
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
#Override
public void onScanCompleted(String path, Uri uri) {
try {
Log.d("onScanCompleted", uri + "success" + conn);
System.out.println("URI " + uri);
if (uri != null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally {
conn.disconnect();
conn = null;
}
}
}