Reputation: 130
i have tried this code to upload an image from gallery to sqllite database in my application but when my application tries to open gallery it gives FORCE TO CLOSE Error and i dont know what is the problem.help me and thanx in advanced
public class ImagggggggActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}}
Upvotes: 1
Views: 16015
Reputation:
Import form device by button click
Intent sdintent = new Intent(Intent.ACTION_PICK);
sdintent.setType("image/*");
startActivityForResult(sdintent, SD_REQUEST);
Get image form sd card
if (requestCode == SD_REQUEST) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
testimage.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
Save Image
DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this);
dbHelper.open();
dbHelper.createUserProfiles( byteArray);
dbHelper.close();
NOw in DatabaseAdapter.java
Define
public static final String U_PIC = "picture";
Then
private long createUserTableContentValues(long id,byte[] byteImage) {
ContentValues values = new ContentValues();
values.put(ID, id);
values.put(U_PIC, byteImage);
return database.insert(IMAGE_INSERT, null, values);
}
I think this might be help you.....
Other resources: http://www.helloandroid.com/tutorials/store-imagesfiles-database
http://www.coderanch.com/t/507054/Android/Mobile/Storing-image-database
Upvotes: 1
Reputation: 24464
Using BLOB data type we can store an image in the SQLite database. The data that actually gets stored in the database is the bytes that make up an image or a file.
As for obtaining pic from gallery, look here, it seems, you have forgotten "super" call.
Upvotes: 1
Reputation: 611
db.execSQL("CREATE TABLE images ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "data BLOB,"
+ "hash BLOB UNIQUE"
+ ");");
To convert the image to a BLOB, use the following code: (Note that we're compressing the image here)
private byte[] getBitmapAsByteArray(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Middle parameter is quality, but since PNG is lossless, it doesn't matter
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
Upvotes: 0