Search code examples
javaandroidandroid-studioimageviewandroid-bitmap

How to get imageView value to insert in to Sqlite?


When I click on my button I can select the photo and it shows in my imageView. But when I click to save, my imageView has become NULL. Anyone can help me? Bitmap always comes null.

I changed

Bitmap bitmap =  
((BitmapDrawable)imageViewProeto.getDrawable()).getBitmap();

to

Bitmap bitmap = imageViewProeto.getDrawingCache();

https://i.sstatic.net/S7gcN.jpg


Solution

  • BitmapDrawable draw = (BitmapDrawable) iv.getDrawable();

    Bitmap bitmap = draw.getBitmap();

    To save Image in Your Gallery :

    FileOutputStream outStream = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
    dir.mkdirs();
    String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File outFile = new File(dir, fileName);
    outStream = new FileOutputStream(outFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    outStream.flush();
    outStream.close();
    

    Refresh Gallery :

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(file));
    sendBroadcast(intent);
    

    And Dont Forget to add permission in your Menifest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />