我想从画廊拍照,我使用此代码。
代码适用于sumsung s4
设备中的某些照片,但在sumsung a7
中无法使用。
我想创建一个适用于所有设备的代码。
public class SignUpPersonalUserStep5 extends Fragment {
public SignUpPersonalUserStep5(PersonalProfil profil) {
this.profil = profil;
}
PersonalProfil profil;
public static final int RESULT_LOAD_PHOTO = 3;
ImageView signUpPhoto;
String ImageDecodableString;
FloatingActionButton button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.step5_sign_up, container, false);
signUpPhoto = (ImageView) view.findViewById(R.id.signUpPhoto);
signUpPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_PHOTO);
}
});
button = (FloatingActionButton) view.findViewById(R.id.step5Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getActivity().getSupportFragmentManager();
SignUpPersonalUserStep4 signUpStep4 = new SignUpPersonalUserStep4(profil);
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack("");
transaction.setCustomAnimations(R.anim.toastox_sheet_show, R.anim.toastox_sheet_hide);
transaction.replace(R.id.signUpForFragment, signUpStep4);
transaction.commit();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_PHOTO && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
ImageDecodableString = cursor.getString(columnIndex);
cursor.close();
Log.d("nizar",ImageDecodableString);
signUpPhoto.setImageBitmap(BitmapFactory
.decodeFile(ImageDecodableString));
// ImageDecodableString = Base64.encodeToString(ImageDecodableString.getBytes(), Base64.DEFAULT);
profil.setImage(ImageDecodableString);
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}}
我想说数据不是空的 请让我知道你的想法 非常感谢你!
答案 0 :(得分:1)
试试这段代码:
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
} else {
Intent intent = new Intent();
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
}
答案 1 :(得分:0)
如果您定位的是Android SDK> = 23,则如果用户拥有Android OS Marshmallow或更高版本,则需要在用户运行时获取权限。
https://developer.android.com/training/permissions/requesting.html
答案 2 :(得分:0)
这会打开Documents应用程序。要允许用户也使用他们可能已安装的任何图库应用程序:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
更多详情here