Android Studio : READ_EXTERNAL_STORAGE permission causes an error - java

I was trying to select an image from a device folder but it an error in the logcat like this:
(https://i.stack.imgur.com/0yFm8.jpg)
(https://i.stack.imgur.com/owWL4.jpg)
This error doesn't always come out and sometimes it changes, but it's always about running out of memory
This is the code with which I choose an image in the gallery:
public void inserimentoImmagine() {
addImage = findViewById(R.id.aggiuntaFoto);
addImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (ActivityCompat.checkSelfPermission(AggiuntaAnimaleManuale.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AggiuntaAnimaleManuale.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
} else {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PICK_FROM_GALLERY:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
} else {
//do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
}
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
if (data == null) {
//Display an error
return;
}
Uri imgUri= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor=getContentResolver().query(
imgUri, filePathColumn, null, null, null
);
String picturePath;
Bitmap bitmap;
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
}
}
}
}

Related

imageView not display an image from external storage

so am trying to make an app and part of the app i have to upload a picture from the phone and display it in imageview and keep the picture on it , i searched and i found a way in a video. the problem is the imageview didnt display the picture . i try many things like change in the code or even change in the xml file like make the imageview wrap content and remove the scaleType but the same no thing change , i understand like 80% from the code but in the last method I get lost so if anyone can help me i will be grateful
java code
public class AddCategory extends AppCompatActivity {
private static final int PERMISSION_REQUEST = 0 ;
private static final int RESULT_LOAD_IMAGE = 0 ;
ImageView imageView ;
Button btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_category);
imageView = findViewById(R.id.imageAddCategory) ;
btn = findViewById(R.id.add_po);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= 23 && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_REQUEST);
}
if (Build.VERSION.SDK_INT >= 23 && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
Intent ii = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ii,RESULT_LOAD_IMAGE);
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case PERMISSION_REQUEST:
if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
Toast.makeText(this," PERMISSION GRANTED" ,Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this," PERMISSION NOT GRANTED" ,Toast.LENGTH_SHORT).show();
finish();
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == RESULT_OK) {
assert data != null;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATE_ADDED};
assert selectedImage != null;
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn
, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
}
}
}
}

pick two images separately in Android

I'm making and android app where I need two upload images and save them to server.
When I select it from the first button, it is appeared in the second but I need two different images.
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
imageView2.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
You are getting this because you are setting selected image to imageview 1 and 2. to solve this you can use different request codes or use Boolean variable to differentiate the uploading of image 1 and 2
Define two Boolean variable to differentiate the uploading of image 1 and 2
Boolean first = false, second = false;
#Override
public void onClick(View v) {
first = true;
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
second = true;
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
if(first)
{
imageView.setImageBitmap(bitmap);
first = false;
}else if(second)
{
imageView2.setImageBitmap(bitmap);
second = false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can use library to select multiple images from Gallery. There are many libraries that are available, one such is https://github.com/ParkSangGwon/TedPicker
Hope this will help you!
Happy Coding
You are setting same image to both ImageViews.
Change Your Code to
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage(GALLERY_REQUEST_CODE_1);
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage(GALLERY_REQUEST_CODE_2);
}
});
}
private void chooseImage(int requestCode) {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE_1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
if (requestCode == GALLERY_REQUEST_CODE_2 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView2.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think you have to set same bitmap in both imageView
You should try bellow code
boolean isFromFirstBtn;
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isFromFirstBtn = true;
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isFromFirstBtn = false;
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
if(isFromFirstBtn){
imageView.setImageBitmap(bitmap);
}else{
imageView2.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Android Studio Request App Permissions cant compile shows error: array dimension missing

When trying to compile Android studio code I get the following error: array dimension missing
error: array dimension missing
public void showCamera(View view) {
if (checkSelfPermission(Manifest.permission.CAMERA) ==PackageManager.PERMISSION_GRANTED) {
showCameraPreview();
}else {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
Toast.makeText(this, "Camera permission is needed to show the camera preview.",
Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]Manifest.permission.CAMERA), REQUEST_CAMERA);
}
}
}
you can try this
public void showCamera(View view){
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.CAMERA},
CAMERA_REQUEST );
}
then you need override onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == CAMERA_REQUEST){
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callCamera();
}else {
Toast.makeText(getApplicationContext(), "Camera permission is needed to show the camera preview.", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
fun calCamera()
public void callCamera()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 200);
}
and override ononActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
super.onActivityResult(requestCode, resultCode, data);
}

how do I make a call Directly when open my contact

the problem when I click on Button choose a contact and choose a contact not call the number, I want when choosing a Number phone call Directly
buttoncontact = findViewById(R.id.choosecontact);
public void choosecontact1 (View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT)
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String call = buttoncontact.getText().toString();
if ((requestCode == 1) && (resultCode == RESULT_OK)) {
Cursor cursor = null;
try {
Uri uri = data.getData();
cursor = getContentResolver().query(uri, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, null, null, null);
if (cursor != null && cursor.moveToNext()) {
String phone = cursor.getString(0);
// the Problem on that code
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(String.valueOf("tel:" + CONTENT_TYPE)));
// i want open phone contact and call directly
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
What is "CONTENT_TYPE"? Shouldn't it be the phone string you just get?
intent.setData(Uri.parse("tel:" + phone));
Add permission in manifest file :
Make function of calling phone number
private static final int CALL_REQUEST = 100;
private void callPhoneNumber(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ProfileActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_REQUEST);
return;
}
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9879879879" ));
startActivity(callIntent);
}
Above api level 23, need to ask runtime permission of call and if user allow it, it will go in below method :
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults)
{
if(requestCode == CALL_REQUEST)
{
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callPhoneNumber();
}
else
{
Toast.makeText(ProfileActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}

How To Detect User Cancel AccountPicker dialog Android Eclipse

i want user Email id via AccountPicker.newChooseAccountIntent. i want Detect User Cancel AccountPicker dialog
here is a code
private static final int REQUEST_CODE_EMAIL = 1;
private TextView email = (TextView) findViewById(R.id.email);
try {
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
// TODO
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
}
}
There is also a RESULT_CANCELED or RESULT_CANCEL constant in an Activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
} else if(requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources