[英]How to passing Image Gallery from fragment into another activity?
将图像从我的画廊传递到另一个活动时遇到问题。 选项活动中的相机片段UI 中的 UI
UI 的屏幕截图在上面。 我已经做了拍照(打开相机)并将图像传递给 UI 2 及其作品,但是从打开的画廊它不能,怎么做?
这是我在CameraFragment 中的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_kamera, container, false);
imageView1 = view.findViewById(R.id.imageView1);
show_graph = view.findViewById(R.id.show_graph);
btnTakepic = view.findViewById(R.id.btnTakepic);
Gofilter = view.findViewById(R.id.Gofilter);
btnTakepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
Gofilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), option.class);
if(imageView1.getDrawable() == null)
{
startActivity(intent);
}else {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
intent.putExtra("image", bitmap);
// intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
}// else {
// BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
// Bitmap imageBitmap = bitmapDrawable.getBitmap();
// intent.putExtra("image2", imageBitmap);
// startActivity(intent);
// }
}
});
return view;
}
private ByteArrayInputStream convertDrawable(ImageView image2){
BitmapDrawable bitmapDrawable = ((BitmapDrawable) image2.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
return new ByteArrayInputStream(imageInByte);
}
这是来自我的OptionActivity
public class option extends AppCompatActivity {
ImageView imageView;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
imageView = findViewById(R.id.imageView2);
Intent intent = getIntent();
// Bundle extras = getIntent().getExtras();
Bitmap bitmap = intent.getParcelableExtra("image");
// Bitmap imageUri1 = intent.getParcelableExtra("imageUri");
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
}
// else {
// Uri imageUri = Uri.parse(extras.getString("imageUri"));
// imageView.setImageURI(imageUri);
// }
// else if (getIntent().getExtras() != null) {
// imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
// imageView.setImageURI(imageUri);
// }
}
}
抱歉英语不好,我希望任何人都可以帮助我谢谢:)
你应该只传递照片的Uri
。 不是整个位图。
Uri getUri(Bitmap yourBitmapToPass) {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "tempPhoto.jpg");
try {
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = yourBitmapToPass;
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Uri.fromFile(file);
}
void open(Intent intent) {
Uri myUri = Uri.parse(intent.getExtras().getString("imageUri"));
}
并以这种方式发送:
intent.putExtra("imageUri", uriToPass.toString());
我也建议使用缓存目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.