将ImageView设置为位图并将其发送到下一个"活动"



大家对Android开发还是个新手。我只是在做一个简单的应用程序,可以检索捕获的图像并将该图像设置为背景。我可以通过使用setBackground(new BitmapDrawable…)来做到这一点。我的下一个活动是编辑文本视图和该背景,每当我点击保存按钮时,它就会进入下一个可以查看编辑后的文本视图和背景的活动。

我的问题是,我可以查看编辑后的文本,但无法查看背景。这是我的以下代码。

RetrieveImage.Activity

public static final String EXTRA_MESSAGE = "com.example.hyunsukcirllee.footstep.MESSAGE";
private ImageView mdisplayBackground;
public static final int SEND_MESSAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_photo_background);
//Bundle to get intent
Bundle extras = getIntent().getExtras();
//If Bundle is not empty proceed
if(extras != null){
//get the imagepass key from the previous activity and set it to bitmap
Bitmap retrieveImage = (Bitmap) extras.get("imagepass");
//If the bitmap is not empty
if(retrieveImage != null){
//Set it to following imageView
mdisplayBackground = (ImageView) findViewById(R.id.background_captured);
mdisplayBackground.setBackground(new BitmapDrawable(getResources(), retrieveImage));
mdisplayBackground.getBackground().setAlpha(80);
}
}
}
//onClick Send button
public void sendMessage(View view){
//Creating an explicit intent to display the sent message
Intent footstep_intent = new Intent(this, DisplayMessageActivity.class);
//EditText Id
EditText editText = (EditText) findViewById(R.id.editText);
//User editText
String message = editText.getText().toString();
//This part is what I want to implement!!!
mdisplayBackground = (ImageView) findViewById(R.id.background_captured);
mdisplayBackground.buildDrawingCache();
Bitmap image_pass_again = mdisplayBackground.getDrawingCache();
//Put both Bitmap and String Message to the intent
footstep_intent.putExtra("Image", image_pass_again);
footstep_intent.putExtra(EXTRA_MESSAGE, message);
startActivity(footstep_intent);
}

}

显示图像和文本的下一个活动

private ImageView mdisplayBbackground;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
//if the intent has the name image do setbackground
if(getIntent().hasExtra("Image")) {
Bundle iimage = getIntent().getExtras();
Bitmap b = (Bitmap) iimage.get("image");
mdisplayBbackground = (ImageView) findViewById(R.id.background_capturedd);
mdisplayBbackground.setBackground(new BitmapDrawable(getResources(), b));
}
//if the intent contains the message-> view
if (getIntent().hasExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE)){
Intent footstep_intent = getIntent();
String message = footstep_intent.getStringExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
}

}

单击"发送"按钮后,以下代码当前不起作用。我用上面的代码搜索了一下位图,并将其设置为ContentURI,但我想我并没有很好地理解这个概念。因此,问题是"是否有办法绕过使用外部存储来调用映像文件的直接路径,为下一个"活动"设置imageView?"另一个问题是"我还在这方面练习,所以如果在编码方面有更好的格式,我想得到一些反馈"谢谢

您不应该在不同的屏幕之间发送位图,这会导致内存泄漏,因为每个位图都应该在工作完成后回收
此外,你不应该自己处理图像加载/处理/缓存等-考虑使用Glide或Picasso这样的库,第一个是谷歌推荐的。

以下是从文件系统加载图像的示例,profileAvatar是一个ImageView:

Glide.with(mContext)
.load(new File(filePath))
.into(profileAvatar);

真的很容易使用;)默认情况下,Glide会在内存中缓存图像。

强烈建议使用不同的方法。

如果你有一部旧手机和一个大位图,它可能不起作用。如果你真的想这样做,这是可能的,但它需要大量内存,而且速度也很慢。相反,如果你真的想这样做,然后通过位图如下

发送位图

try {
//Write file
String filename = "abc.png";
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bmp.recycle();
//Pop intent
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image", filename);
startActivity(in1);
} catch (Exception e) {
e.printStackTrace();
}

接收侧

Bitmap bmp = null;
String filename = getIntent().getStringExtra("image");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}

最新更新