Run the second case with PICK_CONTACT - java

I have this code to put sound in as a contact but when I insert the second case java recognize me if a duplicate any ideas? I've lightened the code so as not to confuse thanks in advance:
code:
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 1;
// first Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT1);
// second
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT2);
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT1):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) { //ecc...
case (PICK_CONTACT2):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c
.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

here is the problem
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 1;
that's why java sees is as duplicate, here is the correct one
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 2;

You have both PICK_CONTACT1 and PICK_CONTACT2 equal to 1:
static final int PICK_CONTACT1 = 1;
static final int PICK_CONTACT2 = 1;
So you're effectively doing:
switch(reqCode) {
case 1:
//stuff
case 1:
//stuff
}
You'll need to make those different values. Also, make sure you add a break at the end of each case.

Related

How would I also be able to get the phone number from the contact the user picked?

I am able to get the contact name that the user chose but not the phonenumber
In the onclick :
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
PICK_CONTACT is just an int value that equals one its my requestcode
then:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if (resultCode == AppCompatActivity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
contactadder.setText(name);
c.close();
}
Here is a little snippet of my work which perhaps might help you to get the right direction. Android database interaction is poorly documented and hard to find the right way/values. Just reply if you need more help to understand. I removed some business relevant data out, so it will not compile this way. Just for easy post!
public static final String[] PROJECTION ={
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.DATA1,
ContactsContract.Data.DATA2,
ContactsContract.Data.DATA3,
ContactsContract.Data.DATA4,
ContactsContract.Data.DATA5,
ContactsContract.Data.DATA6,
ContactsContract.Data.DATA7,
ContactsContract.Data.DATA8,
ContactsContract.Data.DATA9,
ContactsContract.Data.DATA10,
ContactsContract.Data.DATA11,
ContactsContract.Data.DATA12,
ContactsContract.Data.DATA13,
ContactsContract.Data.DATA14,
ContactsContract.Data.DATA15
};
private static final int MIMETYPE_COLUMN=0;
private static final int DATA_COLUMN=1;
private static final int DATATYPE_COLUMN=2;
private static final String SELECTION = ContactsContract.Data.CONTACT_ID + " = ?";
public static ContactWrapper localContact(long contactId){
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,PROJECTION, SELECTION, new String[]{String.valueOf(contactId)}, null);
ContactWrapper wrapper = new ContactWrapper(contact);
if(cursor==null){
return wrapper;
}
while(cursor.moveToNext()){
String mime=cursor.getString(MIMETYPE_COLUMN);
if (mime.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
contact.setU8sDisplayName(cursor.getString(DATA_COLUMN));
} else if (mime.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
putPhoneNumber(contact, cursor);
} else if (mime.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
putEmail(contact, cursor);
} else if (mime.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
putAddress(contact, cursor);
}else if (mime.equals(ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)) {
contact.setU8sSIPAddress(cursor.getString(DATA_COLUMN));
}else if (mime.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
wrapper.setImageBinaryString(getContactPhotoBase64(contactId));
}
}
cursor.close();
return wrapper;
}
private static void putPhoneNumber(ContactWrapper contact,Cursor data){
switch (data.getInt(DATATYPE_COLUMN)){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
contact.setU8sPhoneHome(data.getString(DATA_COLUMN));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
contact.setU8sPhoneMobile(data.getString(DATA_COLUMN));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
contact.setU8sPhoneBusiness(data.getString(DATA_COLUMN));
break;
}
}

Android - picking file with specific extension (pdf & docx)

I used this code for video
public void pickVideo()
{
Intent pickVideoIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoIntent, PICK_VIDEO_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK){
switch (requestCode){
case PICK_VIDEO_ACTIVITY_REQUEST_CODE:
Uri selectedVideo = data.getData();
videoPicked(videoUriToRealPath(selectedVideo));
break;
.
.
.
public String videoUriToRealPath(Uri videoUri){
String[] proj = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(videoUri, proj, null, null, null);
String videoPath = "";
try{
if (cursor != null){
int column_index = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
if (cursor.moveToFirst()){
videoPath = cursor.getString(column_index);
}
}
}finally{
cursor.close();
}
return videoPath;
}
and it's working. I want to set PICK_FILE_ACTIVITY_REQUEST_CODE as one of cases. I've defined it:
public void pickFile()
{
Intent pickFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickFileIntent.setType("*/*");
startActivityForResult(pickFileIntent, PICK_FILE_ACTIVITY_REQUEST_CODE );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK){
switch (requestCode){
case PICK_FILE_ACTIVITY_REQUEST_CODE:
Uri selectedFile = data.getData();
filePicked(fileUriToRealPath(selectedFile));
.
.
.
public String fileUriToRealPath(Uri fileUri){
String[] proj = {MediaStore.Files.Media.DATA};
Cursor cursor = getContentResolver().query(fileUri, proj, null, null, null);
String filePath = "";
try{
if (cursor != null){
int column_index = cursor.getColumnIndex(MediaStore.Files.Media.DATA);
if (cursor.moveToFirst()){
filePath = cursor.getString(column_index);
}
}
}finally{
cursor.close();
}
return filePath;
}
Actually it's not working. extension .PDF and .DOCX in my files. how to fix it?
I think you need to search on the whole sdcard.
searchDir(Environment.getExternalStorageDirectory());
public void searchDir(File dir){
String docExt = ".doc";
String pdfExt = ".pdf";
File listFile[] = dir.listFiles();
if (listFile != null){
for (int i = 0; i < listFile.length; i++){
if (listFile[i].isDirectory()){
searchDir(listFile[i]);
}else{
if(listFile[i].getName().endsWith(docExt)){
//Do what ever u want
}else if(listFile[i].getName().endsWith(pdfExt)){
//Do what ever u want
}
}
}
}
}

How to get name, number and ID from ContactsContract.Contacts in android

I get this error java.lang.IllegalArgumentException: Invalid column data1
This is my code
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
setResult(RESULT_OK, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
TextView phoneTextView = (TextView)findViewById(R.id.textView);
Uri contactData = data.getData();
String[] mProjection =
{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER, // Contract class constant for the word column name
ContactsContract.CommonDataKinds.Phone._ID, // Contract class constant for the locale column name
};
Cursor c = getContentResolver().query(contactData,mProjection, null, null, null);
String contactName = "";
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
contactName = c.getString(0);
Log.v("NAME OF CONTACT", contactName);
}
c.close();
}
}
//break;
}
}
When the code is as above, I get the error message I showed earlier.
However, when I change this piece of code
String[] mProjection =
{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER, // Contract class constant for the word column name
ContactsContract.CommonDataKinds.Phone._ID, // Contract class constant for the locale column name
};
to
String[] mProjection =
{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
};
It works, Log displays the name.
My question here is, why does it do this?

Assigning ID to specific dynamically generated content

I have a template form that is called and put into view programmatically. In the form I have a button that enables the user to search the phone for images and then saves the image. The image thumbnail shows the image selected and saved. The thumbnail is placed in an ImageView with an XML id of "imgView" next to the button. In order to save the image for each dynamic form generated I am, on form creation/retrieval, selecting the "imgView" generated and then assigning it an integer id which starts from 50001, like so:
case R.id.makeLayoutButton:
v1 = vi.inflate(R.layout.form_template, null);
//add view to the insertPoint
((LinearLayout) insertPoint).addView(v1);
mStartActivityButton = (Button)v1.findViewById(R.id.start_file_picker_button1);
mStartActivityButton.setOnClickListener(this);
thumbnailContainer = (ImageView)v1.findViewById(R.id.imgView);
thumbnailContainer.setId(setImageViewID);
imageViewId = thumbnailContainer.getId();
setImageViewID++;
break;
I'm then, when the user selects an image, iterating from 50000 to 500020, then in the loop searching for an ImageView with an id of i, like so:
ImageView imageView = (ImageView)findViewById(i);
And when found, I'm placing the image in the appropriate imageView, like so:
if(i == imageView.getId()){
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
return;
}
Problem is that its crashing and giving a NULL POINTER EXCEPTION error when trying to retrieve and store the image. It seems like its not finding the ImageView with the appropriate ID assigned. Any clue as to where I'm going wrong?
Code that starts image browsing:
case R.id.start_file_picker_button1:
// Create a new Intent for the file picker activity
Intent intent1 = new Intent(this, FilePickerActivity.class);
Intent i1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i1, RESULT_LOAD_IMAGE);
break;
Code for that looks for image and stores it:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
for(int i = 50000;i<=50020; i++){
ImageView imageView = (ImageView)findViewById(i);
if(i == imageView.getId()){
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
return;
}
}
}
}
Solved it. Here is the code if anyone is interested, I realize that this can be optimized and fixed further.
int templateID = 1, inflatedID = 0, setImageViewID = 10, buttonID = 1, imageViewId;
Button b, target, bNext, testTemplate, mStartActivityButton, browseButton;
View v1, insertPoint;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;
ImageView thumbnailContainer;
private TextView mFilePathTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.make_question);
Initialize();
}
public void Initialize(){
//button for adding new forms
b = (Button) findViewById(R.id.makeLayoutButton);
b.setOnClickListener(this);
bNext = (Button) findViewById(R.id.bNext);
bNext.setOnClickListener(this);
//get the template form to be duplicated, v1 is the form layout to be duplicated
vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = vi.inflate(R.layout.form_template, null);
//container where forms will be contained in
insertPoint = findViewById(R.id.questionsContainer);
// Set the views
mFilePathTextView = (TextView)findViewById(R.id.file_path_text_view);
mStartActivityButton = (Button)findViewById(R.id.start_file_picker_button);
mStartActivityButton.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId() == R.id.makeLayoutButton){
v1 = vi.inflate(R.layout.form_template, null);
((LinearLayout) insertPoint).addView(v1);
mStartActivityButton = (Button)v1.findViewById(R.id.start_file_picker_button);
mStartActivityButton.setId(buttonID);
mStartActivityButton.setOnClickListener(this);
thumbnailContainer = (ImageView)v1.findViewById(R.id.imgView);
thumbnailContainer.setId(setImageViewID);
buttonID++;
setImageViewID++;
}
for(int x=1;x<=10;x++){
if(v.getId() == x){
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, v.getId());
}
}
if(v.getId() == R.id.bNext){
Intent b = new Intent(MakeQuestion.this, ChoosingTarget.class);
startActivity(b);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView)findViewById(requestCode + 9);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

Android launcher widget issue

I am making my first android launcher based on a stock launcher. I am trying to add widgets onto my main linear layout. But that doesn't work. It doesn't get furter than the onActivityResult() method. Please help me and thanks so much in advance! My code:
private static final int REQUEST_CREATE_SHORTCUT = 1;
private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
private static final int REQUEST_CREATE_APPWIDGET = 5;
private static final int REQUEST_PICK_APPLICATION = 6;
private static final int REQUEST_PICK_SHORTCUT = 7;
private static final int REQUEST_PICK_LIVE_FOLDER = 8;
private static final int REQUEST_PICK_APPWIDGET = 9;
private static final int REQUEST_PICK_ANYCUT=10;
private static final int REQUEST_SHOW_APP_LIST = 11;
private static final int REQUEST_EDIT_SHIRTCUT = 12;
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.linearLayout);
//Add widgets
void selectWidget() {
int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
addEmptyData(pickIntent);
Log.d("addWidgetProcedure", "Starting activity to pick widget");
startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
}
void addEmptyData(Intent pickIntent) {
ArrayList<AppWidgetProviderInfo> customInfo =
new ArrayList<AppWidgetProviderInfo>();
pickIntent.putParcelableArrayListExtra(
AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
pickIntent.putParcelableArrayListExtra(
AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
Log.d("addWidgetProcedure", "AddEmptyData");
};
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
Log.d("addWidgetProcedure", "inside onActivityResult");
if (resultCode == RESULT_OK ) {
Log.d("addWidgetProcedure", "RESULT_OK");
if (requestCode == REQUEST_PICK_APPWIDGET) {
Log.d("addWidgetProcedure", "calling configureWidget(data)");
configureWidget(data);
}
else if (requestCode == REQUEST_CREATE_APPWIDGET) {
Log.d("addWidgetProcedure", "calling createWidget(data)");
createWidget(data);
}
}
else if (resultCode == RESULT_CANCELED && data != null) {
Log.d("addWidgetProcedure", "RESULT_CANCELED");
int appWidgetId =
data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
private void configureWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo =
mAppWidgetManager.getAppWidgetInfo(appWidgetId);
if (appWidgetInfo.configure != null) {
Intent intent =
new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(appWidgetInfo.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
} else {
createWidget(data);
}
}
public void createWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo =
mAppWidgetManager.getAppWidgetInfo(appWidgetId);
AppWidgetHostView hostView =
mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
linearLayout.addView(hostView);
Toast.makeText(getApplicationContext(), "Adding widget", 15).show();
}
When i press menu->add widget, it calls the selectWidget() method.
All the logcat output from when i press menu->add widget to i have selected one.
07-11 14:38:27.178: W/KeyCharacterMap(1751): Bad keycharmap - filesize=32
07-11 14:38:27.178: W/KeyCharacterMap(1751): Error loading keycharmap file '/system/usr/keychars/bravo-keypad.kcm.bin'. hw.keyboards.0.devname='bravo-keypad'
07-11 14:38:27.178: W/KeyCharacterMap(1751): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-11 14:38:28.348: D/addWidgetProcedure(1751): AddEmptyData
07-11 14:38:28.348: D/addWidgetProcedure(1751): Starting activity to pick widget
07-11 14:38:28.428: D/addWidgetProcedure(1751): inside onActivityResult
Try replacing your:
private static final int REQUEST_PICK_APPWIDGET = 9;
With:
private static final int REQUEST_PICK_APPWIDGET = 0;

Categories