我是新來的Android編程。我想知道如何從圖庫中選擇圖像並將其作爲ImageView背景。 我讓代碼從圖庫中選擇並使其成爲ImageView背景,但每次離開應用程序時圖像都會消失。 我必須保存在數據庫中,但我沒有太多的知識吧如何從圖庫中選擇圖片,並保存爲背景的ImageView,使用SQLite
如果有人能幫助我.. 謝謝
我的Java文件
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());
}
}
}
我的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>