简体   繁体   中英

how select image from gallary and pass it to another activity,

I am making a birthday wisher app, everthing works fine but I want that user can select image from gallary and it passes to 2nd activity, when timer is finised, I used ticker coundown in this.

My MainActivity

public class MainActivity extends AppCompatActivity {

    CircularView circularViewWithTimer;
    EditText entertime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

        entertime = (EditText) findViewById(R.id.Txt_time);

        circularViewWithTimer = findViewById(R.id.circular_view);

        CircularView.OptionsBuilder builderWithTimer =
                new CircularView.OptionsBuilder()
                        .shouldDisplayText(true)
                        .setCounterInSeconds(15)
                        .setCircularViewCallback(new CircularViewCallback() {
                            @Override
                            public void onTimerFinish() {
                               String name = entertime.getText().toString();
                                Intent i = new Intent(MainActivity.this, Bday_page.class);
                               i.putExtra("text", name);
                                startActivity(i);
                                finish();
                            }

                            @Override
                            public void onTimerCancelled() {

                                // Will be called if stopTimer is called
                                Toast.makeText(MainActivity.this, "CircularCallback: Timer Cancelled ", Toast.LENGTH_SHORT).show();


                            }
                        });

        circularViewWithTimer.setOptions(builderWithTimer);
    }

    public void btn_pause(View view) {
        circularViewWithTimer.pauseTimer();
    }

    public void btn_start(View view) {
        MediaPlayer mMediaPlayer;

        if(entertime.getText().toString().isEmpty()){
            Toast.makeText(this, "Enter name of bday boy", Toast.LENGTH_SHORT).show();
        }
        else {
            circularViewWithTimer.startTimer();

            mMediaPlayer = new MediaPlayer();
            mMediaPlayer = MediaPlayer.create(this,R.raw.count);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.start();
        }
    }

    public void btn_resume(View view) {
        circularViewWithTimer.resumeTimer();
    }
}

My 2nd Activity

public class Bday_page extends AppCompatActivity {

    TextView nameuser;
    MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bday_page);
       nameuser = (TextView)findViewById(R.id.txt_imagename);

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer = MediaPlayer.create(this, R.raw.happybady);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.start();

        Intent i = getIntent();
        nameuser.setText(i.getStringExtra("text"));


    }
}

first of all you have to request the storage permission in the manifest

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

after that in your MainActivity make your onTimerFinish methods like these

@Override
                    public void onTimerFinish() {
  Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent,1777);
}

now you requsted the image from Galary then add onActivityResult method to your main activity to receive the image

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if ( resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_REQUEST) {
 Uri uri = data.getData();
            Cursor cursor;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    cursor = getContentResolver().query(uri,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    cursor.close();
String name = entertime.getText().toString();
                        Intent i = new Intent(MainActivity.this, Bday_page.class);
                       i.putExtra("text", name);
                       i.putExtra("imagePath",picturePath )
                        startActivity(i);
                        finish();
}}}

now you get the path of selected image and send it to the next activity and in your 2nd Activity receive the image like these

Intent i = getIntent();
nameuser.setText(i.getStringExtra("text"));
String imagePath = i.getStringExtra("imagePath") //add these

now you have the image Path in the new activity you can do what you want to do with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM