我正在开发一个Android应用程序,用户在点击按钮时激活相机并将该图像设置为ImageView。
直到昨天才完美运作。但是在我将Android版本更新到5.0.1后,ImageView没有显示任何内容。但令人惊讶的是,它仍适用于较低版本。
我已经浏览了几个网站,但仍然没有结果。
这是我的代码段:
//Button Click action
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// start camera activity
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK && intent != null) {
// get bundle
Bundle extras = intent.getExtras();
bitMap = (Bitmap) extras.get("data");
ivThumbnailPhoto.setImageBitmap(bitMap);
}
}
答案 0 :(得分:0)
以下代码将从gallery或camrea获取图像,并且它可以处理所有版本,只能复制camrea的代码
private File photoFile = null;
public void selectImage() {
final Dialog dialog = new Dialog(this, R.style.CustomDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.setCancelable(true);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
final TextView camrea= (TextView) dialog.findViewById(R.id.text);
final TextView lib= (TextView) dialog.findViewById(R.id.text1);
final TextView cancel= (TextView) dialog.findViewById(R.id.text2);
camrea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent() ;
dialog.cancel();
}
});
lib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), 200);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, 300);
}
dialog.cancel();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.show();
}
private void dispatchTakePictureIntent() {
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
if (!marshMallowPermission.checkPermissionForCamera()) {
marshMallowPermission.requestPermissionForCamera();
} else {
if (!marshMallowPermission.checkPermissionForExternalStorage()) {
marshMallowPermission.requestPermissionForExternalStorage();
} else {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
//Log.d("Uri","Activity started");
startActivityForResult(takePictureIntent, 100);
}
}
}
}
private void Setimage(Uri imguri) {
//you can set ur image here using uri
}
private File createImageFile() throws IOException {
@SuppressLint("SimpleDateFormat")
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return File.createTempFile(
imageFileName,/* prefix */
".jpg", /* suffix */
storageDir /* directory */);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
if (requestCode == 100) {
if("com.google.android.apps.docs.storage".equals(Uri.fromFile(photoFile).getAuthority()))
Toast.makeText(RegistrationActivity.this, "File Not On Device", Toast.LENGTH_SHORT).show();
else {
Setimage(Uri.fromFile(photoFile));
}
} else if (requestCode == 200) {
mIsImageUploadUri=Uri.parse(getPathFromURI(getApplicationContext(),Uri.parse(data.getData().toString())));
final Uri selectedImage = Uri.parse(getPathFromURI(getApplicationContext(),data.getData()));
//Log.d(TAG,"Uri"+ selectedImage.toString());
if("com.google.android.apps.docs.storage".equals(selectedImage.getAuthority()))
Toast.makeText(RegistrationActivity.this, "File Not On Device", Toast.LENGTH_SHORT).show();
else {
Setimage(data.getData());
}
}else if (requestCode == 300) {
final Uri originalUri = data.getData();
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//noinspection ResourceType
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
Log.d(TAG,"Uri"+ originalUri.toString());
if("com.google.android.apps.docs.storage".equals(originalUri.getAuthority()))
Toast.makeText(RegistrationActivity.this, "File Not On Device", Toast.LENGTH_SHORT).show();
else {
Setimage(originalUri);
}
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPathFromURI(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}