简体   繁体   English

Select 来自图库的照片并在另一个活动中显示

[英]Select a photo from the gallery and show it in another Activity

I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity.我正在制作一个移动应用程序,我必须通过单击按钮从图库中获取 select 图像,然后将其显示在另一个活动中。 The problem is the image wont show to the second activity.问题是图像不会显示给第二个活动。 Theres no problems running the code.运行代码没有问题。 what would be the problem.. PLEASE HELP ME会有什么问题..请帮助我

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

}

public void buttonGalleryOpen(View view)
{
Intent intent = new         Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    //noinspection deprecation
    startActivityForResult(intent, RESULT_OK);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Bitmap selectedphoto = null;


super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null){

Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null,     null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
 cursor.close();
Intent intent = new Intent(MainActivity.this,gallery_view.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
}
}
}

I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity.我正在制作一个移动应用程序,我必须通过单击按钮从图库中获取 select 图像,然后将其显示在另一个活动中。 The problem is the image wont show to the second activity.问题是图像不会显示给第二个活动。 Theres no problems running the code.运行代码没有问题。 what would be the problem.. PLEASE HELP ME会有什么问题..请帮助我

This is an activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_below="@+id/textview1"
android:layout_marginTop="3dp">

<Button
android:id="@+id/bottonGalleryOpen"
android:onClick="buttonGalleryOpen"

android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="pick" />

</RelativeLayout>


</RelativeLayout>

gallery_view.java Gallery_view.java

public class gallery_view extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_view);
ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
Bitmap selectedImage  =(Bitmap)this.getIntent().getParcelableExtra("data");
imageview.setImageBitmap(selectedImage);

}
}

gallery_view.xml Gallery_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

<RelativeLayout
android:id="@+id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

<RelativeLayout
android:layout_below="@id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/ImageShow"
android:layout_width="match_parent"
android:layout_height="500dp" />
    
</RelativeLayout>

</RelativeLayout>

This line in your code does not make sense:您的代码中的这一行没有意义:

intent.putExtra("data", "selectedphoto");

You are adding here string "selectedphoto" which is in no way connected to selectedphoto variable you initialised earlier.您在此处添加字符串“selectedphoto”,它与您之前初始化的 selectedphoto 变量没有任何关系。 You could put your bitmap to intent extra as byte array but this is in-efficient, especially when the image is large.您可以将您的 bitmap 设置为额外的字节数组,但这效率低下,尤其是当图像很大时。 Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity.不要将 bitmap 传递给 ShowImage 活动,而是传递您的 URI,然后在 ShowImage 活动中检索实际的 bitmap,就像您现在在 PictureOptions 活动中所做的那样。

intent.setData(passed uri here);

In your ShowImage activity do:在您的 ShowImage 活动中执行以下操作:

URI imageUri = getIntent().getData();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 firebase 数据从一个活动传递到另一个活动? - How to pass firebase data from one activity to another? 从图库中选择多张图片,然后将它们上传到 Firebase Storgae - 但出现 ENOENT(没有此类文件或目录)错误 - Select multiple images from gallery and then upload them to Firebase Storgae - but get an ENOENT (No such file or directory) error 如何将recyclerview中的所有数据传递给另一个活动并在listview或recyclerview中显示它 Firebase - How can I pass all the data in the recyclerview to another activity and show it in the listview or recyclerview Firebase 使用 firebase 按路径从数组中获取照片 - Getting photo from array by path with firebase ADF - 迭代 select Output 到复制活动 - ADF - Iterative select Output to Copy activity 为什么我必须 select 2 次(不同的照片)才能在 firebase 存储中显示图像 - Why I have to select 2 times (different photo) for displaying image in firebase storage Select 从最后一个寄存器到另一列中另一个寄存器之前的时间 - Amazon redshift - Select the time from the last register before another register in another column - Amazon redshift 如何将数据从 Activity 传递到 Fragment KOTLIN? - How to pass data from Activity to Fragment KOTLIN? 如何从 Firebase 检索其他活动的数据? - How to retrieve data from Firebase on other activity? 有没有办法在 MicrosoftBot Framework 中识别照片是从相机拍摄的还是从附件拍摄的 - Is there a way to identify whether photo is taken from camera or attachment in MicrosoftBot Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM