@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkSelfPermission();
myHelper = new myDBHelper(this);
btnImage1 = (ImageView) findViewById(R.id.btnImage);
text1 = (TextView) findViewById(R.id.text);
button1 = (Button) findViewById(R.id.button);
//
// BitmapFactory.Options options = new BitmapFactory.Options();
// Bitmap originalBm = BitmapFactory.decodeFile(tempFile.getAbsolutePath(), options);
// btnImage1.setImageBitmap(originalBm);
btnImage1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
startActivityForResult(intent, PICK_FROM_ALBUM);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strNames = null;
sqlDB = myHelper.getReadableDatabase();
Cursor cursor;
cursor = sqlDB.rawQuery("SELECT*FROM address123", null);
//sqlFile = new File(cursor1.getString(1));
while(cursor.moveToNext()) {
strNames += cursor.getString(1);
}
text1.setText(strNames);
Log.d(TAG,text1.getText().toString());
cursor.close();
sqlDB.close();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_ALBUM) {
Uri photoUri = data.getData();
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
assert photoUri != null;
cursor = getContentResolver().query(photoUri, proj, null, null, null);
assert cursor != null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
tempFile = new File(cursor.getString(column_index));
str2 = cursor.getString(column_index);
insert(str2);
Toast.makeText(getApplicationContext(), "입력됨", 0).show();
text1.setText(cursor.getString(column_index)); // 경로
} finally {
if (cursor != null) {
cursor.close();
}
}
setImage();
}
}
// sqlite 입력
public void insert(String address) {
sqlDB = myHelper.getWritableDatabase();
ContentValues value = new ContentValues();
value.put("name",address);
sqlDB.insert("address123",null, value);
}
// 갤러리에서 받아온 이미지 넣기
private void setImage() {
btnImage1 = findViewById(R.id.btnImage);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap originalBm = BitmapFactory.decodeFile(tempFile.getAbsolutePath(), options);
btnImage1.setImageBitmap(originalBm);
}
// sqlite 이용
public class myDBHelper extends SQLiteOpenHelper {
public myDBHelper(Context context) {
super(context, "addressDB",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table address123 (_id integer primary key autoincrement, name text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists address123";
db.execSQL(sql);
onCreate(db);
}
}
sqlite에 imagepath를 넣어 저장하려고 하는데 버튼을 눌러 경로를 불러올때 에러가 나네요...
계속 고민해봤는데도 도저히 모르겠어서 고수님들에게 물어봅니다. 아니면 저의 방식이 잘못된 것인가요?
아직 여기에 구현하지는 않았지만 제가 원하는 것은 id당 image경로를 따로따로 불러오고 싶은데 이거에 대한 방법도 알려주시면 감사하겠습니다.(꾸벅)
정말로 이 에러에 대해 혹은 이 방법에 대해 문제점에 대해 알려주시면 감사하겠습니다. 일주일동안 이 상태에 멈춰 있어요 ㅠㅠ