2013-08-24 119 views
1

我正在(缓慢)制作一个应用程序,显示音调列表,并让用户“长按”某个特定的一个,然后调出一个上下文菜单,询问您是否要将其复制到SD卡。 唯一的问题是最后一部分,我需要帮助。基本上音调存储在Raw文件夹中,我需要它将选定的音调文件复制到SD卡,最好是在通知文件夹中。上下文菜单选择后,从原始文件夹复制文件到SD卡?

只是想知道是否有人能给我一个例子,说明我会如何去做这件事,因为我绝对失去了?

这里是我的代码

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); 
    } 
}} 

回答

0

试试这个。通过调用它:

writeToSD("MyNotes.txt","Some text"); 

...

public void wrtieToSD(String sFileName, String sBody) 
{ 
try 
{ 
    File root = new File(Environment.getExternalStorageDirectory(), "Notes"); 
    if (!root.exists()) 
    { 
     root.mkdirs(); 
    } 

    File gpxfile = new File(root, sFileName); 
    FileWriter writer = new FileWriter(gpxfile); 
    writer.append(sBody); 
    writer.flush(); 
    writer.close(); 

    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
} 
catch(IOException e) 
{ 
    e.printStackTrace(); 
    importError = e.getMessage(); 
    iError(); 
} 
} 

修改此,​​以满足您的需求。

而且您需要的权限:

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

我的方式在我头上我这里:-( –

+1

你必须开始somewhe重新...两个月前,我对Java语言一无所知,Android脱离了这个世界......我只是问了一些问题并进行了研究,调查和研究,现在我可以找到解决方法,了解大部分Java和充分了解Android的结构。每天几个小时绰绰有余。试试这个: 1.你试图达到什么目的?例如...将存储在应用程序中的文件写入SD卡。 2.您是否使用过Google?尝试“Android将原始文件写入SD卡教程”或“Android将原始文件写入SD卡的stackoverflow” 3.做教程他们最好 – 2013-08-26 11:43:03

+0

我想这只是我做了一切错误的方式,像我的数组列表哪我知道不应该在主线程中,但它是我可以在网上找到的唯一教程,很容易理解。不幸的是,我住在苏格兰一个非常偏远的地方,想找人帮忙几乎是不可能的。 –

1
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); 
    } 
}} 
+0

你能否举一个例子说明我会从哪里开始修改以满足我的需求?我的所有“声音剪辑”都列在主线程中,而不是字符串中。 –

+0

如果我付钱给你,你愿意帮忙吗? –

+1

即时通讯不适合雇用,我有一大堆项目即时通讯工作在我自己身上,这是一种您使用的方法,您将放入R.raw.id,以及您想要保存文件的字符串名称,您可以用这些行改变目录和文件类型String path =“/ sdcard/music/[my_package_name] /”; String filename = fName +“。ogg”; – JRowan

相关问题