0
이것은 에뮬레이터에서 작동합니다 (저는 genymotion을 사용합니다).하지만 실제 장치에서는 작동하지 않습니다. 이미지보기는 이미지를 표시하지 않을 것입니다. 경로 파일을 보여주기 때문에 비어 있지 않습니다. 여기에 코드가 있습니다. 응용 프로그램을 시작할 때 파일 (이미지)이 이미지보기에 표시되지 않는 것을 제외하고는 모두 정상적으로 작동합니다. 나는 logcat을 조사해 보았는데 이미지가 왜 나타나지 않는지에 대한 오류는 없다. 활동 결과()에이미지보기는 이미지를 표시하지 않습니다. setImageBitmap()
ImageView img_logo;
protected static final int CAMERA_REQUEST = 1;
protected static final int FILE_REQUEST = 2;
private Uri imageCaptureUri;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_my_account, container, false);
try{
firstname = getArguments().getString("firstname");
lastname = getArguments().getString("lastname");
username = getArguments().getString("username");
cno = getArguments().getString("cno");
email = getArguments().getString("email");
address = getArguments().getString("address");
userID = getArguments().getString("userID");
fullname = (TextView) view.findViewById(R.id.name);
tv_uname = (TextView) view.findViewById(R.id.user_name);
tv_cno = (TextView) view.findViewById(R.id.user_cno);
tv_email = (TextView) view.findViewById(R.id.user_email);
tv_address = (TextView) view.findViewById(R.id.user_address);
//upload photo
startDialog();
DisplayImage();
fullname.setText(firstname + " " + lastname);
tv_uname.setText(username);
tv_cno.setText(cno);
tv_email.setText(email);
tv_address.setText(address);
}catch (Exception e){
e.printStackTrace();
}
return view;
}
방법 startDialog()
private void startDialog(){
final String[] items = new String[] {"From Cam", "From SD Card"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Action: ");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar" + String.valueOf(System.currentTimeMillis())
+ ".jpg");
imageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
intent.putExtra("return data", true);
startActivityForResult(intent, CAMERA_REQUEST);
} catch (Exception ex) {
ex.printStackTrace();
}
dialog.cancel();
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete Action using"), FILE_REQUEST);
}
}
});
final AlertDialog dialog = builder.create();
//image
txt_image_path = (TextView) view.findViewById(R.id.image_path);
img_logo = (ImageView) view.findViewById(R.id.display_image);
img_logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
응답에 대한 감사
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != Activity.RESULT_OK)
return;
Bitmap bitmap = null;
String path = "";
if(requestCode == FILE_REQUEST){
imageCaptureUri = data.getData();
path = getRealPathFromURI(imageCaptureUri);
if(path != null){
bitmap = BitmapFactory.decodeFile(path);
}else{
path = imageCaptureUri.getPath();
}
}else {
path = imageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
img_logo.setImageBitmap(bitmap);
txt_image_path.setText(path);
}
가능한 복제 로그 캣 오류를 찾으려면이 수정 된 코드를 시도 [ImageView는 이미지를 표시하지 않습니다 whe n setImageBitmap()에 의해 설정] (http://stackoverflow.com/questions/6538843/imageview-wont-show-image-when-set-by-setimagebitmap) – Danieboy
URL에서 ur 이미지를로드하려면 picasso를 사용하십시오. 보일러 판 코드를 많이 작성하려면 havig에서 urself를 저장하십시오. http://square.github.io/picasso/ –
URL에서 이미지를로드하지 않고 이미지가 외부 저장 장치 또는 카메라의 전화에 있습니다. –