Losing Uri between implicit camera intents - android

I'm putting together a camera app using implicit intents for a college assignment, and I can't figure out what's going wrong with my Uri. I'm assigning a jpg file, then opening the camera using MediaStore. ACTION_IMAGE_CAPTURE as shown below. When I call selectImage(), I want the chosen viewing app to go straight to my recently taken photo. However, although the photo is saved and visible within the gallery, I'm getting an "Item not found" Toast, and I'm looking at an overview of all the image folders. Where am I going wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoFile = new File(Environment.getExternalStorageDirectory(),"cameraTest.jpg");
outputFile = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", photoFile);
Log.i("debugging",outputFile.getPath().toString());
}
public void capturePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(outputFile);
this.sendBroadcast(mediaScanIntent);
Log.i("debugging",outputFile.getPath().toString());
}
}
public void selectImage(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Log.i("debugging",outputFile.getPath().toString());
intent.setDataAndType(outputFile, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}

Related

I want to add "remove" option in image chooser intent

I want to add remove option in image chooser intent, I successfully added gallery option and camera option and those are working fine but I want to add remove option, if I choose remove option the image should remove from imageview.
Thanks in advance :)
My Code:
public void edit_profile_pic(View view) {
Uri uri = null;
Intent GalleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent CameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
Intent chooserIntent = Intent.createChooser(CameraIntent, "Choose");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {GalleryIntent, CameraIntent});
startActivityForResult(chooserIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
ImageView imageView = (ImageView) findViewById(R.id.profile_profile_picture);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Bitmap bitmap = null;
if(data.getData()!=null)
{
try
{
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
I was able to do this with an intent chooser by creating a new activity with the theme NoDisplay like this:
First create the activity which will be used to notify the main activity that the user wants to delete the image:
public class DeleteProfileImageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent resultIntent = new Intent();
resultIntent.putExtra("remove_image", true);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can specify the icon and label in the manifest. Make sure you also set the theme to NoDisplay to avoid it covering up the actual activity with the intent chooser.
<activity
android:name=".activity.profilesetup.DeleteProfileImageActivity"
android:label="#string/title_activity_delete_profile_image"
android:icon="#drawable/ic_delete_image_red"
android:theme="#android:style/Theme.NoDisplay"
/>
Then you can add it to the intent chooser. I check if the image has been set previously to avoid adding the option when there is no image.
if (imageSet) {
Intent removeImageIntent = new Intent(activity, DeleteProfileImageActivity.class);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {removeImageIntent} );
}
Then you can listen on activity result for intent data with the key specified in the DeleteProfileImageActivity, currently remove_image
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data.hasExtra("remove_image") && imageSet) {
// remove/reset the image here
}
}
You have to create a alert Dailog. There you can give option, and where you can add remove option. On remove option use this:
else if (options[item].equals("Remove")) {
ImageView.setImageDrawable(null);
dialog.dismiss();
}
For more help you can refer http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/

Unable to capture the snap using intent - Android

I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image

Cancel active intent from activity

I have this method that opens the native camera.
public void takePhoto(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri;
photoUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(cameraIntent, CAM_REQUREST);
}
It is working great, but I want to be able to return to my activity if the user forgets to, or if there is something my app needs attention for. Is there a way that I can do this from my activity?
startActivityForResult() will automatically return after the result is obtained, so after the photo is taken in this case. If you want to relaunch your app at any point, you should use startActivity() with your own package in the intent.
Do it like this:
PackageManager p = getPackageManager();
String myPackage = getApplicationContext().getPackageName();
Intent intent = p.getLaunchIntentForPackage(myPackage);
startActivity(intent);
Override onActivityResult in your activity
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.e(TAG, "--onActivityResult MainActivity--");
if (requestCode == CAM_REQUREST && resultCode == RESULT_OK) {
Uri selectedImage = intent.getData();
Log.e(TAG, "Your image :" + selectedImage);
}
}

How to show file chooser when button is clicked?

I am trying to upload image in my app and i want to show some choices when my upload button is clicked. Unfortunately it gives me this error. I dont know what is wrong with the code. I got this from hereTrying open a specific folder in android using intent
thanks in advance!
uploadpic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
openFolder();
}
});
}
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/");
intent.setDataAndType(uri, "images");
startActivity(Intent.createChooser(intent, "Open folder"));
takephoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
profpic.setImageBitmap(imageBitmap);
}
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
You only want the user to select an image, am I right? Then maybe a general file picker isn't whats best suited for your needs.
A quick google search came up with this (also from SO). It seems like that solution is limited to pictures on an SD card.
If you don't want to click the link:
This is how you would start an intent in order to get an image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You'll then need to override onActivityResult(), but this is all described in the link.

Camera intent problem, camera is starting without request

I have a little problem with my camera intent. As I know, when camera orientation is changed, activity is restarted. Okej, I am using the code bellow.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (myApplication)getApplication();
if(savedInstanceState ==null ) getFullImage(null);
else{
String somevalue = savedInstanceState.getString("uri");
getFullImage(somevalue);
}
}
private void getFullImage(String testValue)
{ if(testValue == null){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID()+ ".jpg");
outputFile = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
startActivityForResult(intent, TAKE_PICTURE);
}else
{
outputFile = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(testValue);
outputFile = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, testValue);
startActivityForResult(intent, TAKE_PICTURE);
finishFromChild(getParent());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_CANCELED) {
Log.i(TAG, "Back Button");
finishFromChild(this);
}
else
if(requestCode == TAKE_PICTURE && resultCode == RESULT_OK)
{
//I'm creating new file here (for this question is irelevant)
} catch (IOException e) {
e.printStackTrace();
}
Intent myIntent = new Intent(getBaseContext(), com.test.activities.SaveFileActivity.class);
myIntent.putExtra("image", newPath);
startActivityFromChild(this, myIntent, SAVE_ITEM);
finishFromChild(this);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("uri",outputFile.getPath());
}
After picture is captured, I press DONE button and I go to SaveFileActivity. Everyting works fine, until I try from SaveFIleActivity to go to another activity, then camera is starting again. Where should I look for the problem ? Maybe I should kill camera intent, but when ?
I suspect you want to be using the simpler startActivity() and finish() methods instead of startActivityFromChild() and finishFromChild(). I admit, however, that I'm a bit unclear as to what the use of the ones you are actually for.

Categories

Resources