我在MainActivity中实现了3个标签。第一个选项卡是图像片段,其中实现了2个按钮。第一是打开相机,第二是打开画廊。我想在名为Upload Activity的新活动中显示捕获或选定的图像。
图像片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_images, container, false);
View v = inflater.inflate(R.layout.fragment_images, container, false);
FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File imageFolder = new File(Environment.getExternalStorageDirectory(), "/My Children");
imageFolder.mkdir();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
String timestamp = simpleDateFormat.format(new Date());
File image = new File(imageFolder, timestamp+ ".jpg");
// Uri uriImage = Uri.fromFile(image);
Uri uriImage = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
getActivity().startActivityForResult(intent, TAKE_PICTURE);
}
});
btnFolder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(intent, SELECT_PICTURE);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap cam = (Bitmap) extras.get("data");
Intent i = new Intent().setClass(getActivity(), Upload.class);
i.putExtra("image", cam);
startActivity(i);
}
if (requestCode == SELECT_PICTURE && requestCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Intent i = new Intent(getActivity(), Upload.class);
i.putExtra("image", bitmap);
startActivity(i);
}
}catch (Exception e){
e.printStackTrace();
}
还在MainActivity中添加了此代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.vp_pages + ":" + vp_pages.getCurrentItem() );
if (page != null){
page.onActivityResult(requestCode, resultCode, data);
}
}
这是我想要显示图片的上传课程
public class Upload extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Button btnUpload = (Button) findViewById(R.id.btnUpload);
ImageView imageview = (ImageView) findViewById(R.id.imageview);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
imageview.setImageBitmap(bitmap);
}
}
在上限或选择图像后,图像片段将打开而不是上传活动。
答案 0 :(得分:0)
因为您要从图像片段启动相机或图库图像捕获,所以您只能从那里启动意图。
但问题是当你设置
时intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
onActivityResult中的数据将为null,因为您已经给出了自己的uri。 因此,对于相机请求,您可以通过全局存储来检查相机的温度uri。
Uri cameraUri;
然后
cameraUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
getActivity().startActivityForResult(intent, TAKE_PICTURE);
此时uri指的是一些临时文件。相机只需单击并覆盖该文件即可。
所以现在得到如下结果。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
Intent i = new Intent().setClass(getActivity(), Upload.class);
//here pass the original uri that we stored while starting camera
i.putExtra("image", cameraUri.toString());
startActivity(i);
}
if (requestCode == SELECT_PICTURE && requestCode == RESULT_OK) {
Intent i = new Intent(getActivity(), Upload.class);
//this will return you data in case of open gallery
i.putExtra("image", data.getData().toString());
startActivity(i);
}
}
现在在另一个活动上收到此信息并显示
public class Upload extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Button btnUpload = (Button) findViewById(R.id.btnUpload);
ImageView imageview = (ImageView) findViewById(R.id.imageview);
Intent intent = getIntent();
if(intent==null){
return;
}
Uri imageUri = Uri.parse(intent.getStringExtra("image"));
imageview.setImageURI(imageUri);
}
}