简体   繁体   中英

Save Image and Mp3 from Gallery to Internal Storage Directory Android

So, I am doing a University project whereby I am allowing the user to select an Image from camera/gallery and then also select an mp3. I would like to take these two selected files and save them into an internal storage directory with the same name(only the extension .mp3 / .jpg will differ)

I have managed to get the code to create the folder as well as get an image from the camera/gallery but I cant seem to work out how to save it to the internal storage directory or how to get an mp3 in the same way.

Code to create folder

try{ File folder = new File(Environment.getExternalStorageDirectory() + "/InkousticMedia");
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdir();
        }


    }catch (Exception e){
        Log.e("MODEL", "ERROR: " + e.toString());
    }

Code to get the image from MediaStore :

    public class SaveTattooActivity extends AppCompatActivity {

    public void getPhoto()
    {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent,1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode == 1)
        {
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                getPhoto();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save_tattoo);

        if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }else
        {
            getPhoto();
        }



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && requestCode == RESULT_OK && data != null)
        {
            Uri selectedImage = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                imageView.setImageBitmap(bitmap);


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

I have had a look at other similar questions on stack but they all differ so much and there is no concrete answer that I can seem to work out whereby I can save both the mp3 and image into the directory.

This is the code i used to save the image, check this github repository https://github.com/Rawkush/Status-Saver .

                File f1,f2;
                f1 = new File(uri.toString());
                String fname = f1.getName();
                f2 = new File(Environment.getExternalStorageDirectory()+"/YOUR_FOLDER/");
                f2.mkdirs();

                try {
                    FileUtils.copyFileToDirectory(f1,f2);
                    ContentValues values =new ContentValues();
                    values.put(MediaStore.Images.Media.DATE_TAKEN,System.currentTimeMillis());
                    values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
                    values.put(MediaStore.MediaColumns.DATA,f2.toString()+"/"+fname);
                    getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                }finally {
                    Toast.makeText(getContext(),"Saved",Toast.LENGTH_LONG).show();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM