在应用程序(资源/原始)中设置铃声

时间:2014-05-27 09:44:37

标签: android gridview notifications ringtone

See the photos

在图像中类似的应用程序设置为铃声和通知声音我如何 raw ringtones文件夹。

GridView中有20个单位的鸟类声音。 鸟的图片播放声音按 长按"铃声选择"和#34;通知声音选择"设置必须显示如何。 http://appcrawlr.com/android/sounds-of-birds-ringtones

MainActivity.java;

public class MainActivity extends Activity implements OnItemClickListener {
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        GridView gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new ImageAdapter(this));
        registerForContextMenu(gridView);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View gorunum,
                    int position, long id) {

                if (position == 0) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.american_crow);
                    mp.start();

                    Toast.makeText(MainActivity.this, "American Crow",
                            Toast.LENGTH_SHORT).show();
                }

                if (position == 1) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.american_goldfinch);
                    mp.start();
                    Toast.makeText(MainActivity.this, "American Goldfinch",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 2) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.american_kestrel);
                    mp.start();
                    Toast.makeText(MainActivity.this, "American Kestrel",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 3) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.american_robin);
                    mp.start();
                    Toast.makeText(MainActivity.this, "American Robin",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 4) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.baltimore_oriole);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Baltimore Oriole",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 5) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.barn_swallow);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Barn Swallow",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 6) {
                    if (mp != null) {
                        mp.release();
                        mp = null;

                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.blue_cheeked_bee_eater);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Blue Cheeked Bee Eater",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 7) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this, R.raw.blue_jay);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Blue Jay",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 8) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.brown_headed_nuthatch);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Brown Headed Nuthatch",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 9) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer
                            .create(MainActivity.this, R.raw.budgerigar);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Buggerigar",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 10) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this, R.raw.canary);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Canary",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 11) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.carolina_chickadee);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Carolina Chickadee",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 12) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.carolina_wren);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Carolina Wren",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 13) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.chestnut_sided_warbler);
                    mp.start();
                    Toast.makeText(MainActivity.this,
                            "Chesthunt Sided Warbler", Toast.LENGTH_SHORT)
                            .show();
                }
                if (position == 14) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.chimney_swift);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Chimney Swift",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 15) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.common_grackle);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Comon Grackle",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 16) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this, R.raw.crane);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Crane",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 17) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this, R.raw.cuckoo);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Cuckoo",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 18) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.dark_eyed_junco);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Dark Eyed Junco",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 19) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.downy_woodpecker);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Downy Woodpecker",
                            Toast.LENGTH_SHORT).show();
                }
                if (position == 20) {
                    if (mp != null) {
                        mp.release();
                        mp = null;
                    }
                    mp = MediaPlayer.create(MainActivity.this,
                            R.raw.eastern_bluebird);
                    mp.start();
                    Toast.makeText(MainActivity.this, "Eastern Blue Bird",
                            Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

}
SD卡,权限如何。我什么都不知道。 等待帮助 感谢

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public Integer[] birdPicture = { R.drawable.american_crow,
            R.drawable.american_goldfinch, R.drawable.american_kestre,
            R.drawable.american_robin, R.drawable.baltimore_oriole,
            R.drawable.barn_swallow, R.drawable.blue_cheeked_bee_eater,
            R.drawable.blue_jay, R.drawable.brown_headed_nuthatch,
            R.drawable.budgerigar, R.drawable.canary,
            R.drawable.carolina_chickadee, R.drawable.carolina_wren,
            R.drawable.chestnut_sided_warbler, R.drawable.chimney_swift,
            R.drawable.common_grackle, R.drawable.crane, R.drawable.cuckoo,
            R.drawable.dark_eyed_junco, R.drawable.downy_woodpecker,
            R.drawable.eastern_bluebird

    };

    // Constructor
    public ImageAdapter(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return birdPicture.length;
    }

    @Override
    public Object getItem(int position) {
        return birdPicture[position];
    }

    @Override
    public long getItemId(int position) {
        return (position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        Resources r = Resources.getSystem();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                90, r.getDisplayMetrics());
        ;
        imageView
                .setLayoutParams(new GridView.LayoutParams((int) px, (int) px));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(birdPicture[position]);
        return imageView;
    }

}

所有代码中的应用

1 个答案:

答案 0 :(得分:0)

我认为这就是你的意思,如果不让我知道:

字符串名称=&#34; your_raw_audio_name&#34 ;;

        File file = new File(Environment.getExternalStorageDirectory(),
                "/myRingtonFolder/Audio/");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/myRingtonFolder/Audio/";

        File f = new File(path + "/", name + ".mp3");

        Uri mUri = Uri.parse("android.resource://"
                + context.getPackageName() + "/raw/" + name);
        ContentResolver mCr = context.getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile = mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile = null;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, name);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, f.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);
        values.put(MediaStore.Audio.Media.IS_MUSIC, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
                .getAbsolutePath());
        Uri newUri = mCr.insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE, newUri);
            Settings.System.putString(mCr, Settings.System.RINGTONE,
                    newUri.toString());
        } catch (Throwable t) {

        }