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

huangapple go评论88阅读模式
英文:

Image is not displaying in second activity

问题

当我点击按钮时,画廊打开并选择图像,但图像不显示在第二个活动中。
我尝试了几天,但没有得到结果。请帮我查看完整的代码。
以下是我的代码:

Activity 1

import android.content.Intent;
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;

    @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

public class ImageActivity extends 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);
    }
}
英文:

When I click, button gallery opens and I select image, but image is not showing in second Activity.
I am trying from few days but I didn't get result. Please help me with the full code.
Here is my code:

Activity 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

Public class ImageActivity extends 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);
}}```

答案1

得分: 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.` 错误

```java
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 中的 if 语句:

if (requestCode == RESULT_LOAD_IMAGE)
英文:

In ImageActivity onCreate method

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

decodeUri is a example method(working code only) to resize the image convert to bitmap and set to imageview. if the image size is big you might get 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 &lt; requiredSize || height_tmp / 2 &lt; 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.GalleryImageActivity&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/gallery_image&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.5&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:srcCompat=&quot;@tools:sample/avatars&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

change if statemtemt in onActivityResult

if (requestCode == RESULT_LOAD_IMAGE)

答案2

得分: 0

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

英文:

Try to convert the image to byte array and then pass that to next activity(as ByteArray) and use that.

huangapple
  • 本文由 发表于 2020年9月4日 23:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定