Java 选择关联菜单后,是否将文件从原始文件夹复制到SD卡?
我正在(慢慢地)制作一个应用程序,它显示一系列音调,让用户“长按”某个音调,然后弹出一个上下文菜单,询问您是否想将其复制到SD卡。 唯一的问题是最后一部分,我需要帮助。基本上,音调存储在原始文件夹中,我需要它将所选音调文件复制到SD卡,最好是在通知文件夹中 只是想知道是否有人能给我一个例子,说明我会怎么做,因为我完全迷路了 这是我的密码Java 选择关联菜单后,是否将文件从原始文件夹复制到SD卡?,java,android,copy,Java,Android,Copy,我正在(慢慢地)制作一个应用程序,它显示一系列音调,让用户“长按”某个音调,然后弹出一个上下文菜单,询问您是否想将其复制到SD卡。 唯一的问题是最后一部分,我需要帮助。基本上,音调存储在原始文件夹中,我需要它将所选音调文件复制到SD卡,最好是在通知文件夹中 只是想知道是否有人能给我一个例子,说明我会怎么做,因为我完全迷路了 这是我的密码 import java.util.ArrayList; import android.app.ListActivity; import android.medi
import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Fizz");
s.setSoundResourceId(R.raw.fizz);
mSounds.add(s);
s = new Sound();
s.setDescription("Flipper");
s.setSoundResourceId(R.raw.flipper);
mSounds.add(s);
s = new Sound();
s.setDescription("Glass Key");
s.setSoundResourceId(R.raw.glasskey);
mSounds.add(s);
s = new Sound();
s.setDescription("Halo");
s.setSoundResourceId(R.raw.halo);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();
}@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int length = mSounds.size(); // get the length of mSounds object
String[] names = new String[length]; // creates a fixed array with strings
for(int i = 0; i < length; i++) {
// add sound name to string array
names[i] = mSounds.get(i).getDescription(); // returns the string name
}
switch(item.getItemId()) {
case R.id.copytosd:
Toast.makeText(this, "Applying " + getResources().getString(R.string.copy) +
" for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}}
import java.util.ArrayList;
导入android.app.ListActivity;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.view.ContextMenu;
导入android.view.ContextMenu.ContextMenuInfo;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView.AdapterContextMenuInfo;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了ListActivity{
private ArrayList mSounds=null;
私有SoundAdapter mAdapter=null;
静态MediaPlayer层=空;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//创建一个简单的列表
mSounds=newarraylist();
声音s=新声音();
s、 setDescription(“Anjels”);
s、 setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s=新声音();
s、 设置说明(“嘶嘶声”);
s、 setSoundResourceId(R.raw.fizz);
mSounds.add(s);
s=新声音();
s、 设置描述(“翻转器”);
s、 setSoundResourceId(R.raw.flipper);
mSounds.add(s);
s=新声音();
s、 设置说明(“玻璃钥匙”);
s、 setSoundResourceId(R.raw.glasskey);
mSounds.add(s);
s=新声音();
s、 设置说明(“光环”);
s、 setSoundResourceId(R.raw.halo);
mSounds.add(s);
mAdapter=新的SoundAdapter(此,R.layout.list_行,mSounds);
setListAdapter(mAdapter);
}
@凌驾
public void onListItemClick(ListView父视图、视图v、整数位置、长id){
声音s=(声音)mSounds.get(位置);
MediaPlayer mp=MediaPlayer.create(这是s.getSoundResourceId());
mp.start();
}@凌驾
public void onCreateContextMenu(ContextMenu菜单、视图v、ContextMenuInfo菜单信息){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。上下文菜单,菜单);
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项。getMenuInfo();
int length=mSounds.size();//获取mSounds对象的长度
字符串[]名称=新字符串[长度];//使用字符串创建固定数组
for(int i=0;i
试试这个。请致电:
writeToSD("MyNotes.txt","Some text");
修改此选项以满足您的需要
您还需要以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
请参阅以下链接。这对你有帮助 试试这个而不是你有的那个
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Sound sound = getListAdapter().getItem(info.position);
int length = mSounds.size(); // get the length of mSounds object
String[] names = new String[length]; // creates a fixed array with strings
for(int i = 0; i < length; i++) {
// add sound name to string array
names[i] = mSounds.get(i).getDescription(); // returns the string name
}
switch(item.getItemId()) {
case R.id.copytosd:
Toast.makeText(this, "Applying " + getResources().getString(R.string.copy) +
" for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
boolean saved = saveas(sound.getSoundResourceId(),"filename");
return true;
default:
return super.onContextItemSelected(item);
}
}}
@覆盖
公共布尔值onContextItemSelected(MenuItem项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项。getMenuInfo();
Sound Sound=getListAdapter().getItem(信息位置);
int length=mSounds.size();//获取mSounds对象的长度
字符串[]名称=新字符串[长度];//使用字符串创建固定数组
for(int i=0;i
你能给我举个例子,说明我将从哪里开始根据自己的需要修改它吗?我所有的“声音剪辑”都列在主线程中,而不是字符串中。如果我付钱给你,你愿意帮忙吗?我不是受雇的,我有一大堆项目我自己在做,这是你使用的方法,你输入R.raw.id,以及你想保存文件的字符串名称,您可以使用以下行更改目录和文件类型:String path=“/sdcard/music/[my_package_name]/”;字符串文件名=fName+“.ogg”;如何从关联菜单上的选择中获取raw.id?使用公共int来跟踪onListItemClick的位置,然后在关联菜单中,您将获得该位置,调出声音描述,如果它与您知道的要使用的字符串匹配,我就在这里:-(你必须从某个地方开始……两个月前,我对Java语言一无所知,Android已经不在这个世界上了……我只是问,问,问,问,研究,研究,现在我可以找到自己的路,了解大部分Java,完全理解安卓的结构。每天几个小时就足够了。试试这个:1.什么是你想达到什么目的?例如,将应用程序中存储的文件写入SD卡。2.你用谷歌搜索过吗?试试“Android将原始文件写入SD卡教程”或“Android将原始文件写入SD卡stackoverflow”3.教程是否最好?我想这只是我已经完成了wro的所有工作
public boolean saveas(int ressound, String fName){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/music/[my_package_name]/";
String filename=fName+".ogg";
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String completePath = baseDir + File.separator + "music" + File.separator + "my_package" + File.separator + filename;
boolean exists = (new File(completePath)).exists();
if (!exists){new File(completePath).mkdirs();}
FileOutputStream save;
try {
save = new FileOutputStream(completePath);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "exampletitle");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
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);
//Insert it into the database
this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Sound sound = getListAdapter().getItem(info.position);
int length = mSounds.size(); // get the length of mSounds object
String[] names = new String[length]; // creates a fixed array with strings
for(int i = 0; i < length; i++) {
// add sound name to string array
names[i] = mSounds.get(i).getDescription(); // returns the string name
}
switch(item.getItemId()) {
case R.id.copytosd:
Toast.makeText(this, "Applying " + getResources().getString(R.string.copy) +
" for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
boolean saved = saveas(sound.getSoundResourceId(),"filename");
return true;
default:
return super.onContextItemSelected(item);
}
}}