在我的应用程序中,我想从 gallery 中选择图像,转换为 base64 ,然后设置为imageView
。
我写下以下代码并在LogCat
中向我显示 base64 ,将其设置为imageView
时不向我显示任何图像!
我将{em> base64 代码从logCat
复制到此网站以查看我的图片。
但它显示我的空图像!
测试基地64:https://www.base64decode.org/
我的片段代码:
public class DashBoardFragment extends BaseFragment {
/* @BindView(R.id.addPhoto)
ImageView addPhoto;*/
private Handler handler;
private Context context;
private View v;
private PrefsUtils prefsUtils;
private ImageView addPhoto;
private LinearLayout noLoginFrag_btn;
private TextView textView2, dash_winnerTxt, dash_allBidsTxt, dash_allBidsTxt2, dash_winnerTxt2, dash_notWinTxt, dash_bougthTxt;
private File file;
private Uri uri;
private Intent CamIntent, GalIntent, CropIntent;
public static final int RequestPermissionCode = 1;
private DisplayMetrics displayMetrics;
private int width, height;
private String base64String, base64StringForSite;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
handler = new Handler();
context = getActivity();
prefsUtils = new PrefsUtils(context);
if (prefsUtils.isExist(PrefsKeys.PHONE_NUMBER.name())) {
v = inflater.inflate(R.layout.fragment_dashboard, null);
addPhoto = v.findViewById(R.id.addPhoto);
textView2 = v.findViewById(R.id.textView2);
dash_winnerTxt = v.findViewById(R.id.dash_winnerTxt);
dash_allBidsTxt = v.findViewById(R.id.dash_allBidsTxt);
dash_allBidsTxt2 = v.findViewById(R.id.dash_allBidsTxt2);
dash_winnerTxt2 = v.findViewById(R.id.dash_winnerTxt2);
dash_notWinTxt = v.findViewById(R.id.dash_notWinTxt);
dash_bougthTxt = v.findViewById(R.id.dash_bougthTxt);
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
textView2.setText(Constants.profileResponse.getRes().getUser().getName());
dash_winnerTxt.setText(Constants.profileResponse.getRes().getWinner() + "");
dash_winnerTxt2.setText(Constants.profileResponse.getRes().getWinner() + "");
dash_allBidsTxt.setText(Constants.profileResponse.getRes().getRegistered() + "");
dash_allBidsTxt2.setText(Constants.profileResponse.getRes().getRegistered() + "");
dash_bougthTxt.setText(Constants.profileResponse.getRes().getBought() + "");
dash_notWinTxt.setText(Constants.profileResponse.getRes().getRegistered() - Constants.profileResponse.getRes().getWinner() + "");
Glide.with(context)
.load(Constants.SERVER + Constants.profileResponse.getRes().getUser().getAvatarFile())
.asBitmap()
.centerCrop()
.into(new BitmapImageViewTarget(addPhoto) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
addPhoto.setImageDrawable(circularBitmapDrawable);
}
});
addPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getImageFromGallery();
}
});
} catch (Exception e) {
}
}
}, 1000);
} else {
v = inflater.inflate(R.layout.fragment_no_login_dash, null);
noLoginFrag_btn = v.findViewById(R.id.noLoginFrag_btn);
noLoginFrag_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(context, RegisterPhoneActivity.class));
getActivity().finish();
}
});
}
return v;
}
@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
imageCropFunction();
} else if (requestCode == 2) {
if (data != null) {
uri = data.getData();
imageCropFunction();
}
} else if (requestCode == 1) {
if (data != null) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
base64String = Base64ConverterUtils.convert(bitmap);
Log.e("Base64Image", base64String);
Bitmap bitmap1 = Base64ConverterUtils.convert(base64String);
addPhoto.setImageBitmap(bitmap1);
}
}
}
private void imageCropFunction() {
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 200);
CropIntent.putExtra("outputY", 200);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 3);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, 1);
} catch (ActivityNotFoundException e) {
}
}
private void getImageFromGallery() {
GalIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(GalIntent, " Select image from gallery "), 2);
}
}
我的转换器类:
public class Base64ConverterUtils {
public static Bitmap convert(String base64Str) throws IllegalArgumentException {
byte[] decodedBytes = Base64.decode(
base64Str.substring(base64Str.indexOf(",") + 1),
Base64.DEFAULT
);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
public static String convert(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}
}
我该如何解决?请帮帮我