我正在尝试构建可以使用图像保存receipe的应用程序,然后我们可以在“详细信息”菜单中显示它 但我有一个问题,图像没有出现,我不知道发生了什么,但在数据库中保存的字节。
这是我的保存活动的活动
__init__()
这是我的详细活动:
public class BuatResepActivity extends AppCompatActivity {
protected Cursor cursor;
DataHelper dbHelper;
Button ton1, ton2, butUp;
EditText text1, text2, text3, text4, text5;
ImageView imageResep;
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
String TABLE_NAME = "resep";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buatresep_activity);
dbHelper = new DataHelper(this);
//text1 = (EditText) findViewById(R.id.etID);
text2 = (EditText) findViewById(R.id.etNamaResep);
text3 = (EditText) findViewById(R.id.etBahan);
text4 = (EditText) findViewById(R.id.etLangkah);
ton1 = (Button) findViewById(R.id.buttonSimpan);
ton2 = (Button) findViewById(R.id.buttonBack);
butUp = (Button) findViewById(R.id.upimage);
imageResep = (ImageView) findViewById(R.id.imageViewResep);
butUp.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, "Select Picture"),
SELECT_PICTURE);
}
});
ton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getWritableDatabase();
byte[] byteImage1 = null;
try {
FileInputStream instream = new FileInputStream(selectedImagePath);
BufferedInputStream bif = new BufferedInputStream(instream);
byteImage1 = new byte[bif.available()];
bif.read(byteImage1);
db.execSQL("insert into resep( nama_resep, bahan, langkah, image) values('" +
text2.getText().toString() +"','" +
text3.getText().toString()+"','"+
text4.getText().toString()+"','"+
byteImage1+"')");
Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
HomeActivity.ma.RefreshList();
finish();
} catch (IOException e) {
System.out.println("Error Exception : " + e.getMessage());
}
}
});
ton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String gambar = data.getData().toString();
System.out.println("Data Image : "+gambar);
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageResep.setVisibility(View.VISIBLE);
imageResep.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String path = null;
String[] projection = { MediaStore.Files.FileColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor == null){
path = uri.getPath();
}
else{
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(projection[0]);
path = cursor.getString(column_index);
cursor.close();
}
return ((path == null || path.isEmpty()) ? (uri.getPath()) : path);
}
}