gpt4 book ai didi

c# - xamarin - ImageView 在从画廊的相机拍摄照片后显示黑色图像

转载 作者:行者123 更新时间:2023-11-30 00:48:17 24 4
gpt4 key购买 nike

我遇到了 CircleImageView 的问题从图库中选择图像或从相机中拍摄照片后,它显示黑色图像,它在 lolipop 中工作得很好,但在 kitkat 版本中出现了这个问题

这是我的代码

我的 xml ImageView

 <refractored.controls.CircleImageView
android:id="@+id/storeImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/misr_pharmacy"
/>

我的C#代码

 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;



}
}

我的助手类创建文件

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);
}

最佳答案

不要乱用 GetData() 来构造另一个 uri。

您已经有一个 uri!

直接使用iv_profileImage.SetImageURI(data.Data);

关于c# - xamarin - ImageView 在从画廊的相机拍摄照片后显示黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com