我正在使用Intent呼叫相机和图库。
我叫相机或画廊,但是没有调用onActivityResult()。
我可以拍照,也可以打开图库,但无法显示图片。
我应该在哪里修理?
请教我。
public static class SelectImageDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setMessage("please select").setPositiveButton("camera",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,CAMERA);
}
}).setNegativeButton("gallery",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent=new Intent();
intent.setAction(intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,GALLERY);
}
});
return builder.create();
}
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode==CATEGORY){
if(resultCode==RESULT_OK) {
Bundle bundle = data.getExtras();
textCategory.setText(bundle.getString("item"));
}
}else if(requestCode==GALLERY){
if(resultCode==RESULT_OK){
try{
InputStream is=getContentResolver().openInputStream(data.getData());
Bitmap bitmap=BitmapFactory.decodeStream(is);
imageButton.setImageBitmap(bitmap);
is.close();
}catch (Exception e){
e.printStackTrace();
}
}
}else if(requestCode==CAMERA){
if(resultCode==RESULT_OK){
Bitmap bitmap=(Bitmap)data.getExtras().get("data");
imageButton.setImageBitmap(savePic(bitmap));
}
}
}
public Bitmap savePic(Bitmap bitmap){
String path= Environment.getExternalStorageDirectory().getPath();
String fileName = System.currentTimeMillis() + ".png";
try {
FileOutputStream outputStream = new FileOutputStream(path+"/"+fileName);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
String[] paths = {Environment.getExternalStorageDirectory().getPath()+"/"+fileName};
String[] mimeType = {"image/png"};
MediaScannerConnection.scanFile(getApplicationContext(), paths, mimeType, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String s, Uri uri) {
}
});
return bitmap;
}
最佳答案
这就是我给相机打电话的方式