3

I created a home screen widget (1*1) and I am trying to open the intent chooser for the camera and the gallery from that widget. I tried opening intent chooser from other class but it's not working. Here is the code from my configuration activity:

Intent clickIntent = new Intent(ConfigurationActivity.this, WidgetProviderSmall.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

PendingIntent pendingIntent = PendingIntent.getBroadcast(ConfigurationActivity.this, mAppWidgetId, clickIntent, 0);
views.setOnClickPendingIntent(R.id.img_widget, pendingIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

This is from my AppWidgetProvider class:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction()==null) {
        Bundle extras = intent.getExtras();
        if(extras!=null) {              
            class.OpenIntentChooser();
        }
    }
    else {
        super.onReceive(context, intent);
    }
}

Any suggestions?

Eel Lee
  • 3,434
  • 2
  • 30
  • 45
ramya
  • 368
  • 1
  • 13
  • this : http://stackoverflow.com/questions/11732872/android-how-can-i-call-camera-or-gallery-intent-together – KrishnaJ Aug 19 '16 at 12:05
  • Please provide a [mcve]. That would include the implementation of `OpenIntentChooser()` and an explanation of what "it's not working" means. Also, check your LogCat. If I had to guess, you are not including `FLAG_ACTIVITY_NEW_TASK` on your `Intent` that you are using to start the activity. That is required when starting an activity from a `BroadcastReceiver`. The LogCat message would point this out to you. – CommonsWare Aug 19 '16 at 13:02

1 Answers1

-1

For Camera Code Please Use As Per your need

import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  

public class MainActivity extends Activity {  
     private static final int CAMERA_REQUEST = 1888;  
     ImageView imageView;  
     public void onCreate(Bundle savedInstanceState) {  

         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  

         imageView = (ImageView) this.findViewById(R.id.imageView1);  
         Button photoButton = (Button) this.findViewById(R.id.button1);  

         photoButton.setOnClickListener(new View.OnClickListener() {  

         @Override  
         public void onClick(View v) {  
              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
              startActivityForResult(cameraIntent, CAMERA_REQUEST);  
         }  
        });  
       }  

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      if (requestCode == CAMERA_REQUEST) {  
       Bitmap photo = (Bitmap) data.getExtras().get("data");  
       imageView.setImageBitmap(photo);  
      }  
   }  


}  

For Gallery Code please use as per your need to modify it

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.imgView);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Riyaz Parasara
  • 1,296
  • 12
  • 24