[英]How to send image from one activity to another in kitkat android?
[英]How to send image from one activity to another in android
主要活動
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = ((TextView) view.findViewById(R.id.recipe_title)).getText().toString();
String time = ((TextView) view.findViewById(R.id.time)).getText().toString();
String servings = ((TextView) view.findViewById(R.id.servings)).getText().toString();
String calories = ((TextView) view.findViewById(R.id.calories)).getText().toString();
ImageView thumbnail = (ImageView) view.findViewById(R.id.recipe_img);
Intent intent = new Intent(context, ActivityDetail.class);
intent.putExtra("RecipeTitle", title);
intent.putExtra("RecipeTotalTime", time);
intent.putExtra("RecipeServings", servings);
intent.putExtra("RecipeCalories", calories);
context.startActivity(intent);
}
});
詳細活動
TextView time = (TextView)findViewById(R.id.time_detail);
TextView servings = (TextView)findViewById(R.id.servings_detail);
TextView calories = (TextView)findViewById(R.id.calories_detail);
ImageView thumbnail = (ImageView)findViewById(R.id.image_paralax);
time.setText(getIntent().getExtras().getString("RecipeTotalTime"));
servings.setText(getIntent().getExtras().getString("RecipeServings"));
calories.setText(getIntent().getExtras().getString("RecipeCalories"));
如何將縮略圖從第一個Activity
發送到第二個Activity
。 在第二個“活動”中,縮略圖將顯示在ImageView上。
您不應嘗試通過Intent發送圖像。 而是發送圖像的Uri更好。在第二個活動中,從Uri加載圖像。
我不建議您將位圖傳遞給其他活動,而應該傳遞字符串url,並且在接收活動中可以從Web或文件中加載圖像。
如果您真的只想使用位圖傳遞圖像,則必須先將其轉換為Byte數組,然后再將其添加到意圖中,將其發送出去並進行解碼。
Bitmap bmp = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
然后在接收活動中,您將需要添加以下代碼以接收字節數組:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
現在您有了一個位圖,可以添加setImageDrawable(new BitmapDrawable(mContext.getResources(), bmp));
那就是您需要做的所有事情。
將圖像發送到意圖不是一件好事,您應該嘗試發送圖像的uri。
但是,如果您確實想要這樣做,則可以從圖像視圖中獲取可繪制的位圖:
Bitmap bitmap = ((BitmapDrawable) thumbnail.getDrawable()).getBitmap();
由於位圖是可打包的,因此您可以直接將其作為附加文件發送:
intent.putExtra("RecipeThumbnail", bitmap);
是的,有可能..但不建議
1)首先您必須從該位圖制作位圖和字節數組
Bitmap bitmap = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
2)然后您可以使用意圖傳遞它
intent.putExtra("Thumbnail Image", image);
3)在接收活動中,
byte[] = getIntent.getExtra("Thumbnail Image");
Bitmap bmp = BitmapFactory.decodeByteArray(byte, 0, byteArray.length);
imageView.setImageBitmap(bmp);
像大師一樣更快地完成它:)
傳遞圖像的ID,您便擁有了它的資源,因此,如果活動b可以通過知道ID來加載圖像,那么為什么在Seralisin上提供如此多的信息...
Intent intent = new Intent(context, ActivityDetail.class);
...
intent.putExtra("imageId", R.id.recipe_img);
而另一項活動就足夠了:
ImageView thumbnail = (ImageView) view.findViewById(yourREceivedImageId);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.