#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.btncheck);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
intent.putExtra("extra_pkgname", getPackageName());
startActivity(intent);
}
});
}
It code only navigate and display other permission screen but not working in require permission.
How to enable and disav other permission in mi phone in android studio
Image here
What kind of permissions do you need?
If I understand you correctly, this should help.
in MainActivity:
private final int REQUEST_CODE_PERMISSIONS = 1001;
private final String[] REQUIRED_PERMISSIONS = new String[]{
"android.permission.CAMERA",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.RECORD_AUDIO",
"android.permission.INTERNET",
"android.permission.WRITE_EXTERNAL_STORAGE"};
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (!allPermissionsGranted()) {
createToast(getString(R.string.error_permission));
finish();
}
}
}
private boolean allPermissionsGranted(){
for(String permission : REQUIRED_PERMISSIONS){
if(ContextCompat.checkSelfPermission(this , permission)
!= PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if(allPermissionsGranted()){
Log.d("TAG", "OK");
} else{
ActivityCompat.requestPermissions(this,
REQUIRED_PERMISSIONS,
REQUEST_CODE_PERMISSIONS);
}
...
}
Related
I am really new to AndroidStudio and I'm trying to make a sharing app. My purpose is to have an app that automatically loads up all the images on my phone(or in my phone's gallery, not that important) and display them on screen. Afterwards, I want to click on one of the images over there, and then use a Share button I made to send that photo to another person (it can be an MMS or any other application, the main problem is that this share button transmit the picture I clicked on most recently).
I don't know a lot about the specifics of Android Studio, meaning I know how to code (sort of) but I am unfamiliar with the possibilities of implementing this. My code is below.
GalleryAdapter.java:
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
private Context context;
private List<String> images;
protected PhotoListener photoListener;
public GalleryAdapter(Context context, List<String> images, PhotoListener photoListener) {
this.context = context;
this.images = images;
this.photoListener = photoListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(context).inflate(R.layout.gallery_item, parent, false)
);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String image=images.get(position);
Glide.with(context).load(image).into(holder.image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
photoListener.onPhotoClick(image);
}
});
}
#Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.image);
}
}
public interface PhotoListener{
void onPhotoClick(String path);
}
}
ImagesGallery.java:
public class ImagesGallery {
public static ArrayList<String> listofImages(Context context){
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listofAllImages=new ArrayList<>();
String absolutePathOfImage;
uri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection={MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
String orderBy= MediaStore.Video.Media.DATE_TAKEN;
cursor=context.getContentResolver().query(uri, projection, null, null, orderBy+" DESC");
column_index_data=cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while(cursor.moveToNext()){
absolutePathOfImage=cursor.getString(column_index_data);
listofAllImages.add(absolutePathOfImage);
}
return listofAllImages;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
GalleryAdapter galleryAdapter;
List<String> images;
TextView gallery_number;
private static final int MY_READ_PERMISSION_CODE=101;
#Override
protected void onCreate(Bundle savedInstanceState) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 101);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery_number=findViewById(R.id.gallery_number);
recyclerView=findViewById(R.id.recyclerview_gallery_images);
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_READ_PERMISSION_CODE);
} else {
loadImages();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId()==R.id.share_menu){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uriToImage=Uri.parse("android.resource://com.example.tutorialpaper5/"+R.drawable.test);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.app_name)));
} else{
return super.onOptionsItemSelected(item);
}
return true;
}
private void loadImages(){
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
images=ImagesGallery.listofImages(this);
galleryAdapter=new GalleryAdapter(this, images, new GalleryAdapter.PhotoListener() {
#Override
public void onPhotoClick(String path) {
Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_LONG).show();
}
});
recyclerView.setAdapter(galleryAdapter);
gallery_number.setText("Photos +("+images.size()+")");
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==MY_READ_PERMISSION_CODE){
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "read external storage permission granted", Toast.LENGTH_LONG).show();
loadImages();
} else {
Toast.makeText(this, "Read external storage permission denied", Toast.LENGTH_LONG).show();
}
}
}
}
You will notice that in my onOptionsItemSelected class I just threw in a picture I had in the drawable directory to see if the share button actually works. It does, now I just need some way for it memorize my last clicked photo and throw it in that Intent. But again, I am not sure if that is possible.
There are multiple options, here are two simple solutions just to link your code parts.
Suggestion with minimal changes in your logic:
You can just save the onPhotoClick(String path) to a field that will defined in the activity class scope and then use this field in the onOptionsItemSelected adding some code parts:
public class MainActivity extends AppCompatActivity {
String clickedImagePath = null
// ...
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId()==R.id.share_menu){
if (clickedImagePath != null) {
// The code you had for sharing with changing line for uriToImage
Uri uriToImage=Uri.parse(clickedImagePath);
} else {
// maybe show some error than nothing selected
}
}
}
// ...
public void onPhotoClick(String path) {
Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_LONG).show();
clickedImagePath = path
}
// ...
Maybe a better UX
Remove the onOptionsItemSelected completely and move the intent code to the onPhotoClick listener.
#Override
public void onPhotoClick(String path) {
Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_LONG).show();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uriToImage=Uri.parse(path); // <-- Don't forget to fix the path
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
MainActivity.this.startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.app_name)));
}
Code for picking an image from gallery
private void pickImageFromGallery() {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK_CODE);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case PERMISSION_CODE:{
if(grantResults.length >0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
pickImageFromGallery();
}else {
Toast.makeText(this,"access denied", Toast.LENGTH_LONG).show();
}
}
}
}
To handle the picked image, i inflated the layout_ticket which contains image view.Assigned the image to it which i picked.
// handle the picked image
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(resultCode==RESULT_OK && requestCode==IMAGE_PICK_CODE){
LayoutInflater mInflater = getLayoutInflater();
View myView = mInflater.inflate(R.layout.layout_ticket, null);
ImageView imageview = (ImageView) myView.findViewById(R.id.imageview);
imageview.setImageURI(data.getData());
}
}
}
After login successful this returns to Main2Activity and I can log in again and again.but start app again after closing and removing from recent list it start in MainActivity. how to directly navigate Main2Activity to MainActivity after if condition is true.
Main2Activity code
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
int AUTHUI_REQUEST_CODE = 1001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
startActivity(new Intent(this, MainActivity.class));
this.finish();
}
}
public void loginRegister(View view) {
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build()
);
Intent intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setTosAndPrivacyPolicyUrls("https://example.com","https://example.com")
.setLogo(R.drawable.i789)
.build();
startActivityForResult(intent, AUTHUI_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTHUI_REQUEST_CODE){
if (requestCode == RESULT_OK){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.d(TAG, "onActivityResult: "+ user.getEmail());
if (user.getMetadata().getCreationTimestamp() == user.getMetadata().getLastSignInTimestamp()){
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Welcome back again", Toast.LENGTH_SHORT).show();
}
Intent t = new Intent(this,MainActivity.class);
startActivity(t);
this.finish();
}else{
IdpResponse response =IdpResponse.fromResultIntent(data);
if (response == null){
Log.d(TAG, "onActivityResult: the user has cancelled the sign in request");
}else {
Log.e(TAG, "onActivityResult: ",response.getError() );
}
}
}
}
}
code- Main Activity
public class MainActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void startLogin(){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
finish();
}
public void signout(View view){
AuthUI.getInstance().signOut(this);
}
#Override
protected void onStart() {
super.onStart();
FirebaseAuth.getInstance().addAuthStateListener(this);
}
#Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(this);
}
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
startLogin();
return;
}
firebaseAuth.getCurrentUser().getIdToken(true)
.addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
#Override
public void onSuccess(GetTokenResult getTokenResult) {
Log.d(TAG, "onSuccess: "+getTokenResult.getToken());
}
});
}
}
I will suggest you that you create a splash screen (startup screen) which will initially on start check for the authentication whether user is logged in or not from the server. All this will be handled in a background thread and the ui thread will carry on the splash screen animation(if you wish any).So if the user is logged in, you navigate to mainActivity else navigate to loginActivity. Hope this helps you.
I've one MainActivity, and few fragments on it.
I am trying to change a ImageView in a fragment using Gallery. But once image is selected, the MainActivity get refresh, which changes the MainActivity back to the default fragment.
Getting permission
textViewChangeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"Select Image executed");
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE);
}
});
On Request permission
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
Log.d(TAG,"Select Image Permission executed");
if(requestCode == REQUEST_CODE){
progressDialogUploadDp.setMessage("Uploading...");
progressDialogUploadDp.show();
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("imageUpdate","true");
startActivityForResult(intent,REQUEST_CODE);
}else{
Toast.makeText(getActivity(), "No Permission Granted", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
On Activity Result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null){
try {
Uri uri = data.getData();
StorageReference filepath =
storageReference.child("Photos").child("DPs").child(ID).child("profilepicture.jpg");
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageviewpropic.setImageBitmap(bitmap);
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialogUploadDp.hide();
progressDialogUploadDp.dismiss();
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Looking forward for a solution!
Updated ---
MainActivity
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null) {
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("questioners")) {
adapter = new SectionStatePagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment(), "HomeFragment");
viewPager.setAdapter(adapter);
}}
}
Back form gallery , activity will run onActivityResult then it will run onResume .
so you can set a FLAG at onActivityResult and make an IF in onResume to set your fragment.
From what you've said it looks like your activity is calling onCreate method and setting default fragment again. What you can do is to in onCreate() create new fragment only if there is no saved state.
if(savedInstanceState==null) setFragment(fragment);
import android.preference.PreferenceFragment;
import android.support.v13.app.FragmentCompat;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
public class Preferences extends PreferenceFragment implements OnPreferenceClickListener, FragmentCompat.OnRequestPermissionsResultCallback {
if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{ android.Manifest.permission.WRITE_EXTERNAL_STORAGE },
Constant.WRITE_STORAGE_PERMISSION);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grants) { }
I'm trying to get the new permissions. The dialog shows up requesting the permission when it should, but onRequestPermissionResult it's never called, be it rejection or acceptance.
Any idea? The examples are all using Activity and not a Fragment, so that might be the problem. Also, I'm not sure which Compat I should be using (v4 vs v13). Min SDK is 14.
full code for you:
public interface PermissionResultListener {
public void onPermissionResult(int requestCode,
String permissions[], int[] grantResults);
}
public class Preferences extends PreferenceFragment implements Preference.OnPreferenceClickListener,
PermissionResultListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void requestPermission() {
Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) activity;
mainActivity.requestPermissionStorage();
mainActivity.setPermissionResultListener(this);
}
}
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
#Override
public void onPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
// request permission result here
}
}
public class MainActivity extends AppCompatActivity {
private PermissionResultListener mPermissionResultListener;
public void setPermissionResultListener(PermissionResultListener mPermissionResultListener) {
this.mPermissionResultListener = mPermissionResultListener;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
Preferences preferences = new Preferences();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
preferences.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, preferences).commit();
}
}
public void requestPermissionStorage() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
Constant.WRITE_STORAGE_PERMISSION);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (mPermissionResultListener != null) {
mPermissionResultListener.onPermissionResult(requestCode, permissions, grantResults);
}
}
}