lundi 5 octobre 2015

How do choose picture from gallery and save as background ImageView, using SQLite

I'm new to Android programming. I would like to know how to choose a image from gallery and put it as the ImageView background. I made the code to choose from gallery and make it the ImageView background, but the image disappears every time I leave the application. I must be saved in the database, but i dont have much knowledge about it

If someone can help me .. Thank you

My Java file

    ImageView btn1, btn2;
    Uri photoPath;


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


        btn1 = (ImageView) findViewById(R.id.imageView1);


        btn1.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.createChooser(intent, "Altere o botão"), 1);
            }
        });

}

    public void onActivityResult(int reqCode, int resCode, Intent data){

        if(resCode == RESULT_OK){
            if(reqCode == 1){
                btn1.setImageURI(data.getData());
            }
        }
    }

My XML File

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.camera.MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    />

</LinearLayout>

Aucun commentaire:

Enregistrer un commentaire