I capture photo and save into my Gallery. Everything works perfect when I capture an image and save. But if the camera is on and if I press the back button without taking a picture then the application stops.
How I can solve this issue?
This is my code:
public class ImportCard extends Activity {
private static final int CAMERA_PIC_REQUEST = 1111;
ImageButton importimage;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_import_card);
ImageButton importimage = (ImageButton) findViewById(R.id.importimage);
importimage.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
//2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
//3
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/vcard"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mapcard.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.mapcard.Main"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.mapcard.NewCard"
android:label="#string/title_activity_new_card" >
</activity>
<activity
android:name="com.example.mapcard.ImportCard"
android:label="#string/title_activity_import_card">
</activity>
<activity
android:name="com.example.mapcard.CardBox"
android:label="#string/title_activity_card_box" >
</activity>
<activity
android:name="com.example.mapcard.QRCode"
android:label="#string/title_activity_new_card" >
</activity>
<activity
android:name="com.example.mapcard.Template"
android:label="#string/title_template" >
</activity>
<activity
android:name="com.example.mapcard.DisplayCard"
android:label="#string/title_display_card" >
</activity>
<activity
android:name="com.example.mapcard.GoogleMap"
android:label="#string/title_activity_google" >
</activity>
<activity
android:name="com.example.mapcard.ViewCard"
android:label="#string/title_view_card" >
</activity>
<activity
android:name="com.example.mapcard.QRScan"
android:label="#string/title_activity_import_card" >
</activity>
<activity
android:name="com.example.mapcard.ViewTemplates"
android:label="#string/title_view_templates" >
</activity>
<activity
android:name="com.example.mapcard.SendEmail"
android:label="#string/title_send_email" >
</activity>
</application>
Error in my LogCat:
java.lang.RuntimeException: Failure delivering result
Error on my device:
Unfortunately, App has stopped
You are checking only requestCode. You should check the value of resultCode also. When the user has successfully performed the action, the resultCode will be equal to RESULT_OK. If the user presses the back button then the resultCode will be RESULT_CANCELED. So your code must be modified like this.
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
//2
Add
android:noHistory="false"
in AndroidManifest.xml for that activity
In onActivityResult you should check whether thumbnail is null, and if so just return.
Edit: like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
Bundle extras = data.getExtras();
if (extras == null || extras.get("data") == null) return;
Bitmap thumbnail = (Bitmap) extras.get("data");
...
}
}
Related
I am using an intent to Gallery from my android application for the user to choose a photo and choose as his profile picture. In my firebase storage, there are some images, but they are not getting displayed in the gallery for that user. I am not able to figure out what is the issue. Kindly help. Here are the things that I have tried so far.
ProfileActivity.java
private static final int RC_PHOTO_PICKER = 3;
galleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
final StorageReference photoref = storageReference.child(userId).child(selectedImageUri.getLastPathSegment());
photoref.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Picasso.with(getContext()).load(taskSnapshot.getDownloadUrl()).into(circularProfilePhoto);
databaseReference.child("profile").setValue(taskSnapshot.getDownloadUrl().toString());
Toast.makeText(getContext(), "Profile Picture Set", Toast.LENGTH_SHORT).show();
}
});
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="#drawable/logoga"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.SplashScreen" />
<activity android:name=".activities.SignInActivity" />
<activity android:name=".activities.SignUpActivity" />
<activity android:name=".activities.ContactDetailsActivity" />
<activity android:name=".activities.AddGuardianActivity" />
<activity android:name=".activities.MainMenuActivity" />
<activity android:name=".activities.ResetPasswordActivity" />
</application>
And here is my Firebase storage. Please find the image below
Firebase Storage Reference
Any help is appreciated. Thanks in advance!
I am trying to Scan the QR code using com.google.zxing.client.android.SCAN but it does not opening the camera and always responding with "Scan was Cancelled!" toast message for Sony Xperia C only.
ScanQR.java
ScanBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Toast toast = Toast.makeText(this, "Content:" + contents,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Toast toast = Toast.makeText(this, "Scan was Cancelled!",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
}
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.ScanQR"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Try something like this.
startActivityForResult(
new Intent(
Emailtry.this,
com.google.zxing.client.android.CaptureActivity.class),
11);
Android app issue:
I have MainActivity that when you press a button it opens the camera (an OCR) via Intent and startActivity. The problem is when I press the button in my mobile the first time after the installation, it gets blocked.
Then, after the first time, it works perfect...
By the way, when I try to execute the app in the android emulator it gives me only one error: "error opening trace file: No such file or directory" I don't know if this error is related with my problem in the mobile.
Here is the intent of MainActivity
Button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent i = new Intent(getApplicationContext(), CaptureActivity.class);
startActivityForResult(i, REQUEST_CODE);
} catch (Exception e) {
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("ocrResult")) {
EditText.setText(data.getExtras().getString("ocrResult"));
} else if (resultCode == RESULT_CANCELED) {
}
}
}
And here you have the code of the OCR that sends the result to MainActivity
public void onClick(View v) {
finish();
}
public void finish() {
Intent data = new Intent();
data.putExtra("ocrResult", ocrResultView.getText());
setResult(RESULT_OK, data);
super.finish();
}
Finally, the manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.sfsu.cs.orange.ocr"
android:installLocation="auto"
android:versionName="0.5.13"
android:versionCode="32"
>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="13"/>
<supports-screens android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.screen.landscape"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden"
>
</activity>
<activity android:name="edu.sfsu.cs.orange.ocr.PreferencesActivity"> </activity>
</application>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
Thank you very much!!
This is totally weird and I've searched through the forums.
In my main class I have a button onClick will launch the contacts application as shown below. When I click the button, he contacts list is shown but as soon as I tap on a contact a security exception is thrown.
public void selectContacts(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String phoneNumber = getPhoneNumber(id);
listOfContacts.add(phoneNumber);
}
} else {
System.out.println("User didn't pick a contact");
finish();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
I've checked the manifest and have tried all combinations of placing the uses-permission tag, within the application, activity etc. But nothing works. Here's my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.company.letsmeet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permision android:name="android.permission.SEND_SMS" />
<uses-permision android:name="android.permission.RECEIVE_SMS" />
<uses-permision android:name="android.permission.INTERNET" />
<uses-permision android:name="android.permission.READ_CONTACTS" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".LetsmeetActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The security exception :
05-04 11:26:39.950: E/AndroidRuntime(3861): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/7 }} to activity {in.company.letsmeet/in.company.letsmeet.LetsmeetActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.SemcContactsProvider2 uri content://com.android.contacts/contacts/7 from pid=3861, uid=10093 requires android.permission.READ_CONTACTS
Any help will be greatly appreciated. I'm trying this on Android 2.1 Sony experia S10 mini.
So guess what, I added the READ_CONTACTS permission in the Manifest GUI and it works. God I wasted an entire day on this. Hope others don't have to.
Try out below code.That may be helpful to you -
public class PickDemo extends Activity {
private static final int PICK_REQUEST=1337;
private static Uri CONTENT_URI=null;
static {
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (CONTENT_URI==null) {
Toast
.makeText(this, "We are experiencing technical difficulties...",
Toast.LENGTH_LONG)
.show();
finish();
return;
}
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.pick);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i=new Intent(Intent.ACTION_PICK, CONTENT_URI);
startActivityForResult(i, PICK_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode==PICK_REQUEST) {
if (resultCode==RESULT_OK) {
startActivity(new Intent(Intent.ACTION_VIEW,
data.getData()));
}
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1"
android:versionName="1.0"
package="com.commonsware.android.contacts.pick"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="6" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false" />
<application android:icon="#drawable/cw"
android:label="#string/app_name">
<activity android:label="#string/app_name"
android:name=".PickDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And, just refer Commonsware's example. Hope this helps you.
KitActivity:
/** in KitActivity, handler invoked after successful transmission **/
private final Handler txHandle = new Handler() {
#Override
public void handleMessage(Message msg) {
boolean success = msg.getData().getBoolean("success");
dismissDialog(DIALOG_TX_PROGRESS);
if(success) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt("previous_scale", mScaleSpn.getSelectedItemPosition());
editor.commit();
//clearFields();
//showDialog(DIALOG_ETX);
KitActivity.this.setResult(0);
KitActivity.this.finish();
} else {
removeDialog(DIALOG_FAIL);
showDialog(DIALOG_FAIL);
}
}
};
MainActivity:
/** in the MainActivity **/
public void startCreateKit() {
Intent i = new Intent(MainActivity.this, KitActivity.class);
startActivityForResult(i,0);
}
protected void onActivityResult(int reqCode, int resCode) {
if(reqCode==0) {
if(resCode==0) {
//we good, perform sync
showDialog(DIALOG_TX_PROGRESS);
Toast.makeText(this, "Performing Auto Sync", Toast.LENGTH_LONG).show();
updateKits();
} else {
//uh oh
}
}
}
createKitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(context, "NEW KIT", Toast.LENGTH_SHORT).show();
startCreateKit();
}
});
onActivityResult is never called in MainActivity. this is pretty much by the book. what's the issue?
stuff i've tried:
- using Activity.RESULT_OK for the result code (which translates to -1);
- removing the setResult() and finish() calls from the handler and calling an outside method to invoke them.
i don't see anything wrong. here's the manifest, nothing awry here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.conceptualsystems.kitmobile"
android:versionCode="8"
android:versionName="#string/version">
<application android:label="#string/app_name" android:icon="#drawable/icon" android:debuggable="true">
<activity android:name="MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ShipActivity"
android:label="Ship Kits"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="KitActivity"
android:label="Kit Entry"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="ColorActivity"
android:label="Color Selection"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
what gives?
in the activity you start (KitActivity.class) on success you do this
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
else you put RESULT_CANCELED instead of RESULT_OK
replaced:
protected void onActivityResult(int reqCode, int resCode)
with:
protected void onActivityResult(int reqCode, int resCode, Intent intent)
amazing what happens when you review the documentation... -_-