创建sdcard:通过命令行使用mksdcard.exe 创mysdcrad.img向sdcard中添加内容:两种可行方案,一种启动虚拟机后直接在eclipse的file explorer的视图中找到/mnt/sdcard文件夹,再点击图标即可;另外还可以通过adbshell中使用adb push文件+对应的sdcard路径
添加虚拟器中读sdcard文件的可写
编写jni调用可参考
http://blog.csdn.net/leixiaohua1020/article/details/47056365
javah -classpath D:\Android4.0\workspace\SFFmpegAndroidHelloWorld\bin\classes -bootclasspath D:\Android4.0\android-sdk\platforms\android-14\android.jar com.example.sffmpegandroidhelloworld.Android_FFmpeg_Decoder_Activity
-classpath 寻找.class文件路径
-bootclasspath android.jar包路径
目标:实现交互式sdcard文件预览,点击选中视频文件,并生成mkv视频格式。
package com.paad.todolist;
import com.example.sffmpegandroidhelloworld.Android_FFmpeg_Decoder_Activity.MyBroadcastReceiver;
import android.app.ListFragment;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
//创建一个对应链表类;
public class FileListFragment extends ListFragment {
public int selectid;
public fileItem selectfile=null;
public void onListItemClick(ListView l, View v, int position, long id) {
Log.println(Log.DEBUG, "we are", "ClickListItem"); //添加链表点击打印;
selectfile=(fileItem)getListView().getItemAtPosition(position);
Intent selctIntent=new Intent(MyBroadcastReceiver.EXTRA_FILTER);
selctIntent.putExtra("UPDATE_TRANSFILE", "transfile");
selctIntent.putExtra("TEXT_VALUE",selectfile.getFilename());
getActivity().sendBroadcast(selctIntent); //绑定过的Fragment;
}
//返回选中的值;
public fileItem getSelectItem(){
return selectfile;
}
}
package com.paad.todolist;
public class fileItem {
String filename;
String Type;
public fileItem(){
filename=null;
Type=null;
}
public fileItem(String filename, String type) {
super();
this.filename = filename;
Type = type;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
@Override
public String toString() {
return "fileItem []";
}
}
package com.paad.todolist;
import java.util.List;
import com.example.sffmpegandroidhelloworld.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyArrayAdapter extends ArrayAdapter {
int resource;
public MyArrayAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
public MyArrayAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
Log.println(Log.DEBUG, "we are", "init9");
resource=textViewResourceId;
Log.println(Log.DEBUG, "we are", "init10");
// TODO Auto-generated constructor stub
}
public View getView(int position,View convertView,ViewGroup parent){
Log.println(Log.DEBUG, "we are", "init11");
LinearLayout fileView;
fileItem item =getItem(position);
String filename =item.getFilename();
String fileType=item.getType();
if(convertView==null){
fileView=new LinearLayout(getContext());
String inflater=Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li=(LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, fileView,true);
Log.println(Log.DEBUG, "we are", "init11");
}else{
fileView=(LinearLayout)convertView;
}
TextView fileText=(TextView)fileView.findViewById(R.id.filename);
TextView TypeText=(TextView)fileView.findViewById(R.id.filetype);
fileText.setText(filename);
TypeText.setText(fileType);
Log.println(Log.DEBUG, "we are", "init12");
return fileView;
}
}
package com.example.sffmpegandroidhelloworld;
import java.io.File;
import java.util.ArrayList;
import com.paad.todolist.FileListFragment;
import com.paad.todolist.MyArrayAdapter;
import com.paad.todolist.fileItem;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Android_FFmpeg_Decoder_Activity extends Activity{
private FileListFragment listViewer;
private MyArrayAdapter fileItemsAdapter;
private ArrayListfillist;
protected TextView cmdEdittext=null;
//添加广播接收;
public class MyBroadcastReceiver extends BroadcastReceiver{
public final static String EXTRA_NAME="UPDATE_TRANSFILE";
public final static String EXTRA_KEY="TEXT_VALUE";
public final static String EXTRA_FILTER="UI_TRANS";
@Override
public void onReceive(Context context, Intent intent) {
String receivename=intent.getStringExtra(EXTRA_NAME);
if(receivename.equals("transfile")){
//String的值如果不是new String,如果都是相同的字符串就会放在一个地方;
String receiverText=intent.getStringExtra(EXTRA_KEY);
cmdEdittext.setText(receiverText);
String pstr="you";
}
}
}
private IntentFilter filter=new IntentFilter(MyBroadcastReceiver.EXTRA_FILTER);
private MyBroadcastReceiver receiver=new MyBroadcastReceiver();
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(receiver);
}
//采用匿名内部类的方式;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.println(Log.DEBUG, "we are", "init0");
super.onCreate(savedInstanceState);
Log.println(Log.DEBUG, "we are", "init1");
setContentView(R.layout.decode_ffmpeg_activity);
final Button filePrev=(Button)findViewById(R.id.pre_file); //选择文件;
final Button startButton=(Button)findViewById(R.id.after_file); //开始进行文件转换;
cmdEdittext= (TextView)findViewById(R.id.filetoTrans1);
Log.println(Log.DEBUG, "we are", "init2");
filePrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
fillist.clear();
Log.println(Log.DEBUG, "we are", "init14");
final File file=Environment.getExternalStorageDirectory();
final File[] files = file.listFiles();
Drawable currentIcon=null;
// TODO Auto-generated method stu;
for (File currentFile : files)
{
fileItem p=new fileItem();
if (currentFile.isDirectory()){
currentIcon = getResources().getDrawable(R.drawable.folder);
p.setFilename(currentFile.getName());
p.setType("folder");
}
else
{
String fileName = currentFile.getName();
p.setFilename(fileName);
if(checkEndsWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingImage)))
{
currentIcon = getResources().getDrawable(R.drawable.img);
p.setType("img");
}
else if(checkEndsWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingVideo))){
currentIcon = getResources().getDrawable(R.drawable.video);
p.setType("video");
}
else if(checkEndsWithInStringArray(fileName,getResources().getStringArray(R.array.fileEndingAudio))){
currentIcon = getResources().getDrawable(R.drawable.audio);
p.setType("audio");
}
else
p.setType("default");
}
if(fillist!=null){
fillist.add(p);
Log.println(Log.DEBUG, "we are", "init15");
}
fileItemsAdapter.notifyDataSetChanged();
}
}
});
Log.println(Log.DEBUG, "we are", "init3");
fillist=new ArrayList();
Log.println(Log.DEBUG, "we are", "init4");
int resId=R.layout.file_image1;
Log.println(Log.DEBUG, "we are", "init5");
fileItemsAdapter=new MyArrayAdapter(this, resId, fillist);
Log.println(Log.DEBUG, "we are", "init6");
FragmentManager fm=getFragmentManager();
listViewer=(FileListFragment)fm.findFragmentById(R.id.FileListFragment);
listViewer.setListAdapter(fileItemsAdapter);
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String cmdline="ffmpeg -i"+" "+Environment.getExternalStorageDirectory()+"/"+cmdEdittext.getText().toString()+ " "+Environment.getExternalStorageDirectory()+"/"+cmdEdittext.getText().toString().substring(0,cmdEdittext.getText().toString().lastIndexOf('.'))+".mkv";
Log.println(Log.DEBUG, "we are", cmdline);
String[] argv=cmdline.split(" ");
Integer argc=argv.length;
ffmpegcore(argc,argv);
}
});
}
protected void onResume(){
super.onResume();
Log.println(Log.DEBUG, "we are", "init0");
registerReceiver(receiver,filter);
}
public native int ffmpegcore(int argc,String[] argv);
private boolean checkEndsWithInStringArray(String checkItsEnd,String[] fileEndings){
for(String aEnd:fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
static {
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
System.loadLibrary("avcodec-57");
System.loadLibrary("avformat-57");
System.loadLibrary("avfilter-6");
System.loadLibrary("avdevice-57");
System.loadLibrary("decoder_transfer");
}
}
源码分享地址:http://pan.baidu.com/s/1bKf94i 密码:kfa1