我有一个打开画廊的按钮,我有另一个打开手机的相机,当图像加载到imageview时,它显示一个textview说“图像上传”,如果没有加载,我使用.seterror来显示消息“上传图片”或其他内容。我已经在不同的手机上进行了测试,它可以在kitty kat和marshmallow上使用,但是当我在牛轧糖手机上测试时,图片似乎没有附加,我收到了.seterror消息,“上传图片”。
清单上的:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST && resultCode == Activity.RESULT_OK && data.getData() != null){
Toast.makeText(TerminosYC.this.getActivity(), "si hay foto", Toast.LENGTH_LONG).show();
//Bitmap bitmap = (Bitmap) data.getExtras().get("data");
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
//imageView.setImageBitmap(bitmap);
}else if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPath(Uri uri) {
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getActivity().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(TerminosYC.this.getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
return;
if (ActivityCompat.shouldShowRequestPermissionRationale(TerminosYC.this.getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(TerminosYC.this.getActivity(), new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(TerminosYC.this.getActivity(), "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(TerminosYC.this.getActivity(), "you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
class btnTakePhotoClicker implements Button.OnClickListener{
@Override
public void onClick(View view) {
/* if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
1313);
}*/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAM_REQUEST);
}
}