Android Activity之间传递图片

Android Activity之间传递图片

直接复制代码就可以使用了

两种方法
这是第一个MainActivity 布局代码 里面的ShapeableImageView 是另一个图片控件 需要接入依赖
依赖为:

//权限请求框架
    implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation "io.reactivex.rxjava2:rxjava:2.0.0"

    //热门强大的图片加载器
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

    //Google Material控件,以及迁移到AndroidX下一些控件的依赖
    implementation 'com.google.android.material:material:1.2.0'

依赖是在:这是个强大的大佬

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <com.google.android.material.imageview.ShapeableImageView
       android:id="@+id/image_one"
       android:src="@drawable/guoguo"
       android:layout_width="wrap_content"
       android:layout_height="500dp"/>
      <Button
       android:id="@+id/button_one"
       android:onClick="tao"
       android:text="跳"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

      </Button>
</LinearLayout>


第二个Activity 布局 就创建一个接收的就好了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/image_one_name"
        android:layout_width="wrap_content"
        android:layout_height="300dp"/>
    <TextView
        android:textColor="#F10808"
        android:layout_marginTop="50sp"
        android:text="第二个Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

第一个Activity 代码

package com.yaolu.mylxsc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.google.android.material.imageview.ShapeableImageView;

import java.io.ByteArrayOutputStream;


public class MainActivity extends AppCompatActivity {

    private ShapeableImageView imageView;
    private Button button;
    //TODO 第二种方法
    private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image_one);
        button = findViewById(R.id.button_one);
        //TODO 第二种方法
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.guoguo);

    }

    public void tao(View view) {
        //TODO 第一种方法
//        Intent intent  = new Intent(MainActivity.this , Main2Activity.class);
//        imageView.setDrawingCacheEnabled(Boolean.TRUE);
//        intent.putExtra("BITMAP", imageView.getDrawingCache()); //这里可以放一个bitmap
//        startActivity(intent);

        //TODO 第二种方法
        Intent intent = new Intent();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100,out);
        byte [] bitmapByte =out.toByteArray();
        intent.putExtra("bitmap", bitmapByte);
        intent.setClass(MainActivity.this, Main2Activity.class);
        startActivity(intent);
    }
}

方法来自于这个哥哥

第二个Activity 代码

package com.yaolu.mylxsc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

import com.google.android.material.imageview.ShapeableImageView;

public class Main2Activity extends AppCompatActivity {

    private ShapeableImageView imageView;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        //TODO 第一种方法
//        Intent intent = getIntent();
//        if (intent  != null &&  intent.getParcelableExtra("BITMAP") != null) {
//            Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");
//            imageView.setImageBitmap(bitmap);

        //TODO 第二种方法
        imageView = findViewById(R.id.image_one_name);
                 Intent intent=getIntent();
                 if(intent!=null)
                     {
                         byte[] bitmapByte = intent.getByteArrayExtra("bitmap");
                         Bitmap bitmap= null;
                         bitmap= BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length, null);
                         imageView.setImageBitmap(bitmap);

                     }
    }

}




希望对你有帮助

作为一个平台

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值