图片不在第二个活动中显示

时间:2020-09-04 15:12:14

标签: java android android-studio photo photo-gallery

单击时,将打开按钮库,并选择图像,但是图像未在第二个活动中显示。 我从几天开始尝试,但是没有得到结果。请帮助我完整的代码。 这是我的代码:

活动1

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;

public class HomepageActivity extends AppCompatActivity {


    private static final int SELECT_PHOTO = 1;
    private static int RESULT_LOAD_IMAGE = 1;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        ImageButton buttonLoadImage = findViewById(R.id.btnphoto);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
                gallery.setType("image/*");
                startActivityForResult(gallery, RESULT_LOAD_IMAGE);
            }

        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_PHOTO) {
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                if (selectedImage != null) {
                    Intent gallery = new Intent(HomepageActivity.this, ImageActivity.class);
                    Bundle extras = new Bundle();
                    extras.putString("image", selectedImage.toString());
                    gallery.putExtras(extras);
                    startActivity(gallery);

                }
            }
        }
}}```

Activity 2

公共类ImageActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    Bundle extras = getIntent().getExtras();
    String name = extras.getString("image");
    Uri imageFinal=Uri.parse(name);

}}```

2 个答案:

答案 0 :(得分:0)

在ImageActivity onCreate方法中

  ImageView imageView = findViewById(R.id.gallery_image);

   Bundle bundle = getIntent().getExtras();
   if(bundle != null) {
       String uriString = bundle.getString("image");
       Uri uri = Uri.parse(uriString);
       try {
           imageView.setImageBitmap(decodeUri(this,uri, 300));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
   }

decodeUri是一个示例方法(仅适用于工作代码),用于调整图像大小,将其转换为位图并设置为imageview。如果图像很大,您可能会得到java.lang.RuntimeException: Canvas: trying to draw too large(168709632bytes) bitmap.

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
        throws FileNotFoundException {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

    int width_tmp = o.outWidth
            , height_tmp = o.outHeight;
    int scale = 1;

    while(true) {
        if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
}

activity_gallery_image.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_height="match_parent"
tools:context=".GalleryImageActivity">

<ImageView
    android:id="@+id/gallery_image"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:srcCompat="@tools:sample/avatars" />
    </androidx.constraintlayout.widget.ConstraintLayout>

更改onActivityResult中的statemtemt

if (requestCode == RESULT_LOAD_IMAGE)

答案 1 :(得分:0)

尝试将图像转换为字节数组,然后将其传递给下一个活动(作为ByteArray)并使用它。

相关问题