Reputation: 21452
I face issue with CircleImageView after selecting image from gallery or take pic from Camera it show black image , it works just fine in lolipop but this issue raised in kitkat version
here is my code
my xml imageView
<refractored.controls.CircleImageView
android:id="@+id/storeImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/misr_pharmacy"
/>
my C# code
private void ChooseImage()
{
String[] items = { "Take Photo", "Choose from Library", "Cancel" };
using (var dialogBuilder = new AlertDialog.Builder(this))
{
dialogBuilder.SetTitle("Add Photo");
dialogBuilder.SetItems(items, (d, args) => {
//Take photo
if (args.Which == 0)
{
if ((int)Build.VERSION.SdkInt >= 23)
{
if (!marshMallowPermission.checkPermissionForWriteExternalStorage())
{
bool _ShouldShowRequest = ShouldShowRequestPermissionRationale(PublicKeys.WriteExternalStoragePermission);
marshMallowPermission.requestPermissionForWriteExternalStorage(_ShouldShowRequest);
}
}
dispatchTakePictureIntent();
}
//Choose from gallery
else if (args.Which == 1)
{
if ((int)Build.VERSION.SdkInt >= 23)
{
if (!marshMallowPermission.checkPermissionForReadExternalStorage())
{
bool _ShouldShowRequest = ShouldShowRequestPermissionRationale(PublicKeys.ReadExternalStoragePermission);
marshMallowPermission.requestPermissionForReadExternalStorage(_ShouldShowRequest);
}
}
dispatchChoosePictureIntent();
}
});
dialogBuilder.Show();
}
}
private void dispatchTakePictureIntent()
{
var takePictureIntent = new Intent(MediaStore.ActionImageCapture);
File photoFile = null;
try
{
photoFile = FileUtils.createImageFile();
currentFilePath = "file:" + photoFile.AbsolutePath;
}
catch (Exception e)
{
return;
}
if (photoFile != null && takePictureIntent.ResolveActivity(PackageManager) != null)
{
takePictureIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(photoFile));
this.StartActivityForResult(takePictureIntent, REQUEST_CAMERA);
}
}
private void dispatchChoosePictureIntent()
{
var intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
intent.SetType("image/*");
this.StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), SELECT_FILE);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
if (requestCode == REQUEST_CAMERA)
{
// Handle the newly captured image
Android.Net.Uri imageUri = Android.Net.Uri.Parse(currentFilePath);
iv_profileImage.SetImageURI(imageUri);
}
else if (requestCode == SELECT_FILE)
{
// Handle the image chosen from gallery
string selectedImagePath = FileUtils.GetPath(this, data.Data);
iv_profileImage.SetImageURI(Android.Net.Uri.FromFile(new File (selectedImagePath)));
}
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
switch (requestCode)
{
case PublicKeys.WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE:
{
if (grantResults[0] == Permission.Granted)
{
//Permission granted
Snackbar.Make(layout, "camera permission is available", Snackbar.LengthShort).Show();
dispatchTakePictureIntent();
}
else
{
//Permission Denied :(
//Disabling functionality
Snackbar.Make(layout, GetString(Resource.String.camera_permission_denied), Snackbar.LengthShort).Show();
}
}
break;
case PublicKeys.READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE:
{
if (grantResults[0] == Permission.Granted)
{
//Permission granted
var snack = Snackbar.Make(layout, "camera permission is available", Snackbar.LengthShort);
snack.Show();
dispatchChoosePictureIntent();
}
else
{
//Permission Denied :(
//Disabling functionality
var snack = Snackbar.Make(layout, "Camera permission is denied.", Snackbar.LengthShort);
snack.Show();
}
}
break;
}
}
my helper class to create file
public static File createImageFile()
{
//create an image file name
string timeStamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
string imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures);
File image = File.CreateTempFile(
imageFileName,
".jpg",
storageDir
);
// Save a file: path for use with ACTION_VIEW intents
return image;
}
public static string GetPath (Context context , Android.Net.Uri uri)
{
bool isKitKat = Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat;
// DocumentProvider
if (isKitKat && DocumentsContract.IsDocumentUri(context, uri))
{
// ExternalStorageProvider
if (isExternalStorageDocument(uri))
{
string docId = DocumentsContract.GetDocumentId(uri);
string[] split = docId.Split(':');
string type = split[0];
if (string.Equals("primary" , type , StringComparison.OrdinalIgnoreCase))
{
return Android.OS.Environment.ExternalStorageDirectory + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri))
{
string id = DocumentsContract.GetDocumentId(uri);
Android.Net.Uri contentUri = ContentUris.WithAppendedId(
Android.Net.Uri.Parse("content://downloads/public_downloads"), long.Parse(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri))
{
string docId = DocumentsContract.GetDocumentId(uri);
string[] split = docId.Split(':' );
string type = split[0];
Android.Net.Uri contentUri = null;
if ("image".Equals(type))
{
contentUri = MediaStore.Images.Media.ExternalContentUri;
}
else if ("video".Equals(type))
{
contentUri = MediaStore.Video.Media.ExternalContentUri;
}
else if ("audio".Equals(type))
{
contentUri = MediaStore.Audio.Media.ExternalContentUri;
}
string selection = "_id=?";
string[] selectionArgs = new string[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if (string.Equals("content" , uri.Scheme , StringComparison.OrdinalIgnoreCase))
{
return getDataColumn(context, uri, null, null);
}
// File
else if (string.Equals("file", uri.Scheme, StringComparison.OrdinalIgnoreCase))
{
return uri.Path;
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static string getDataColumn(Context context, Android.Net.Uri uri, string selection,
string[] selectionArgs)
{
ICursor cursor = null;
string column = "_data";
string[] projection = {
column
};
try
{
cursor = context.ContentResolver.Query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.MoveToFirst())
{
int column_index = cursor.GetColumnIndexOrThrow(column);
return cursor.GetString(column_index);
}
}
finally
{
if (cursor != null)
cursor.Close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static bool isExternalStorageDocument(Android.Net.Uri uri)
{
return "com.android.externalstorage.documents".Equals(uri.Authority);
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static bool isDownloadsDocument(Android.Net.Uri uri)
{
return "com.android.providers.downloads.documents".Equals(uri.Authority);
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static bool isMediaDocument(Android.Net.Uri uri)
{
return "com.android.providers.media.documents".Equals(uri.Authority);
}
Upvotes: 1
Views: 815
Reputation: 11224
Do not mess around with GetData()
to construct another uri.
You have already an uri!
Use iv_profileImage.SetImageURI(data.Data);
directly.
Upvotes: 1
Reputation: 21452
just put your CircleImageView
inside container in my case FrameLayout
and set layerType
to software
to be like this android:layerType="software"
here is my xml code
<FrameLayout
android:id="@+id/imageContainer"
android:layerType="software"
android:layout_width="110dp"
android:layout_height="105dp"
android:elevation="4dp"
android:layout_marginTop="35.3dp"
android:layout_centerHorizontal="true">
<refractored.controls.CircleImageView
android:id="@+id/storeImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/misr_pharmacy"
/>
</FrameLayout>
hope this help any one face same issue
Upvotes: 0