0
我已經創建了一個活動,它提供了相機設備的標準程序。創建圖片後,它會存儲在手機內存中,並將圖片的名稱存儲到數據庫中。問題是,如果您創建了一些照片,那麼數據庫中照片和記錄的名稱不匹配。 例如: 該協議棧:Android。 onActivityResult錯誤
10-21 20:22:37.617: I/System.out (22 016): Debug - name of foto - 70,134,596
10-21 20:22:45.195: I/System.out (22 016): Debug - name of foto - 89,410,152
10-21 20:22:45.382: I/System.out (22 016): Debug - name of database record - 89,410,152
在這種情況下,創建了名稱的照片 - 70134596,但數據庫中獲取一個記錄號 - 89410152.下面的代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = null;
date = (int) (Math.random() * 100000000);
photo = new File(Environment.getExternalStorageDirectory()
+ "/DebtorMini", date + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
System.out.println("Debug - name of foto - " + date);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = new ImageView(ctx);
setContentView(imageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap = null;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
DataBaseHelper helper = new DataBaseHelper(this);
try {
helper.createDataBase();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Bundle extras = getIntent().getExtras();
String category = extras.getString("category");
String address = extras.getString("address");
Toast.makeText(
this,
"Фотография успешно создана категория - " + category
+ " адресс - " + address, Toast.LENGTH_SHORT)
.show();
helper.openDataBase();
helper.exec("INSERT INTO 'photos' (`id`, `address`, `category`) VALUES ('"
+ date + "', '" + address + "', '" + category + "')");
System.out.println("Debug - name of database record - " + date);
helper.close();
}
}
}
如何解決這個問題?
感謝您的回答,但可以作爲查詢媒體提供商獲得的最後一個文件的媒體存儲在的樣品名稱? – Kyborg2011
在答案中添加了代碼,以完成所要求的操作。 –