0

I am currently making a simple mobile project, which is uploading image to my server. But i want to know, how can i get an image of a selected song from gallery in my device.In simple terms, how to put the cover image that i have chosen from gallery(intent) into my imageView, i do not need the song, i jjust need the cover picture. There is no error, i am just asking a way to do it, based on my project. Because i don't understand how to do it. The code bellow is what i have written and it is not functioning at all. Please help me to find the solution, thanks in advance everyone.

My code :

public class MyRecoSong extends Fragment {
   ImageView songImage;
   EditText songTitle, singer;
   Spinner sGenre;
   Button save, update;
   ListView recoSong;
   ArrayList<HashMap<String, String>> songList;
   String serverurl = "http://trinandadinantio.com";
   SongObject song[];
   TextView songID;
   String selectedPath = "";
   ProgressDialog prgDialog;
   MediaMetadataRetriever mmr = new MediaMetadataRetriever();
   byte[] rawArt;
   Bitmap art;
   BitmapFactory.Options bfo = new BitmapFactory.Options();
   List<String> Albumid = new ArrayList<>();
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       return inflater.inflate(R.layout.activity_my_reco_song, container,
               false);
   }
   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
       songList = new ArrayList<>();
       songImage = getActivity().findViewById(R.id.imageView4);
       singer = getActivity().findViewById(R.id.singerTf);
       sGenre = getActivity().findViewById(R.id.spinner3);
       save = getActivity().findViewById(R.id.saveBtn);
       update = getActivity().findViewById(R.id.updateBtn);
       recoSong = getActivity().findViewById(R.id.songLv);
       Bundle bundle = getActivity().getIntent().getExtras();
       String username = bundle.getString("username");
       prgDialog = new ProgressDialog(getActivity());
       prgDialog.setCancelable(false);
       songImage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent_upload = new Intent();
               intent_upload.setType("audio/*");
               intent_upload.setAction(Intent.ACTION_GET_CONTENT);
               startActivityForResult(intent_upload, 1);
           }
       });
   }
   private void initView() {
       songImage = getActivity().findViewById(R.id.imageView4);
       singer = getActivity().findViewById(R.id.singerTf);
       sGenre = getActivity().findViewById(R.id.spinner3);
       save = getActivity().findViewById(R.id.saveBtn);
       update = getActivity().findViewById(R.id.updateBtn);
       recoSong = getActivity().findViewById(R.id.songLv);
   }
   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK) {
           if (requestCode == 1) {
               Cursor cursor = getAc tivity().getContentResolver().query(
                       MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                       new String[]{MediaStore.Audio.Albums._ID,
                               MediaStore.Audio.Albums.ALBUM_ART},
                       MediaStore.Audio.Albums._ID + "=?",
                       new String[
                               {String.valueOf(MediaStore.Audio.Media.ALBUM_ID)},
                       null);
               if (cursor.moveToFirst()) {
                   String path =
                           cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                   int displayWidth =
                           getActivity().getWindowManager().getDefaultDisplay().getWidth();
                   int displayHeight =
                           getActivity().getWindowManager().getDefaultDisplay().getHeight();
                   songImage.getLayoutParams().height = displayHeight / 4;
                   songImage.getLayoutParams().width = displayWidth;
                   Bitmap imageBitmap = BitmapFactory.decodeFile(path);
                   imageBitmap = ThumbnailUtils.extractThumbnail(imageBitmap,
                           800, 600);
                   songImage.setImageBitmap(imageBitmap);
                   songImage.buildDrawingCache();
               }
           }
       }
   }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • What error you are getting with code ? – Wasim K. Memon Dec 14 '18 at 09:45
  • Possible duplicate of [Get cover picture by song](https://stackoverflow.com/questions/15314617/get-cover-picture-by-song) – Manohar Dec 14 '18 at 10:07
  • There is no error, i am just asking a way to do it, based on my project. Because i don't understand how to do it. I am very sorry if there is a misunderstanding. The code above is what i have written and it is not functioning at all. What should i write in the on post result ? – Trinanda Dinantio Dec 14 '18 at 11:36
  • @Redman, i have read that page, but it didn't work, i might did a logical error/mistake there because since there is no error. How can i implement that in my code ? What should i write in my on post execute ? Thanks – Trinanda Dinantio Dec 14 '18 at 11:40
  • You will be getting a bitmap at the end , save bitmap into storage and upload it to server using the path – Manohar Dec 14 '18 at 11:45

0 Answers0