package com.example.myapplication;
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.ImageView;
import android.widget.Toast;
import java.io.InputStream;
public class PhotoActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 0;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image);
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
try{
InputStream in = getContentResolver().openInputStream(data.getData());
Bitmap img = BitmapFactory.decodeStream(in);
in.close();
imageView.setImageBitmap(img);
}catch(Exception e)
{
}
}
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(this, "사진 선택 취소", Toast.LENGTH_LONG).show();
}
}
}
}
위와같이 mainactivy를 구성했는데 앱을 종료하면 사진값이 초기화됩니다. 해결방법 아시는분있나요?
부탁드립니다...
아래는 xml 코드입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/image"
android:layout_width="217dp"
android:layout_height="255dp"
android:layout_gravity="center"
app:srcCompat="@mipmap/ic_launcher" />