How to store the audio/Video file to SQLite database in android.?

11,493

Store the media files in internal/external storage, and store the path to it in your database.

see Saving Files

Share:
11,493
chethankumar
Author by

chethankumar

Updated on June 04, 2022

Comments

  • chethankumar
    chethankumar almost 2 years

    Am new to developing, am successfully store the images to database by converting the images in to bytes array and store it to SQLite database in BLOB (find below code). But am not getting how to store audio's/video's into DB any one help me PLZ...

     public void getImage(View view){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/* video/*");
        startActivityForResult(Intent.createChooser(intent, "select picture"), 1);
    
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == RESULT_OK){
            Uri imageUri = data.getData();
            String imagePath = getPath(imageUri);
    
            image1.setVisibility(View.VISIBLE);
    
            image1.setImageURI(imageUri);
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            Toast.makeText(getApplicationContext(), ""+bitmap, Toast.LENGTH_SHORT).show();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            img = byteArrayOutputStream.toByteArray();
        }
    }
    
     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);
    }
    
    public void saveImage(View v){
    
        imageDatabase = new ImageDatabase(this);
    
        imageDatabase.insert("first",img);
    
        Toast.makeText(getApplicationContext(), ""+imageDatabase.numberOfRows(),Toast.LENGTH_LONG).show();
    }
    

    In above code am taking the image from sdcard and displaying in ImageView and storing into DB.

  • Prerak Sola
    Prerak Sola over 8 years
    Please don't post link as an answer. In future the link may change which will make this answer useless.
  • Renaud Favier
    Renaud Favier over 8 years
    You're right, but I can't really do better. All I can do is suppress this sentense
  • chethankumar
    chethankumar over 8 years
    Renaud thanks for your replay, can you help me out by sending the any example code for this...
  • Renaud Favier
    Renaud Favier over 8 years
    where are the audio/video files located ? how do you get them ? with a chooser like your image example ?
  • chethankumar
    chethankumar over 8 years
    Audio files are located in sdcard, any way by chooser or by give the path of the audio file. (better from chooser).