
您好昨天,我在将文件上传到服务器(https://www.youtube.com/watch?v=K48jnbM8yS4)时看到了一个使用okhtpp3的教程,我按照该教程进行了学习,并且运行正常,但是现在我正在尝试开发一个具有录制按钮来录制音频并将其保存到内部存储以及另一个应用的应用按钮上传音频文件.
但是它没有上传文件.
我的应用程式码:
public class MainActivity extends AppCompatActivity{ private String n=""; private button mRecordBtn,uploadBtn; private TextVIEw mRecordLabel; private MediaRecorder mRecorder; private String mfilename = null; private static final String LOG_TAG = "Record_log"; final int REQUEST_PERMISSION_CODE =1000; private ProgressDialog progress; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); if(!checkPermissionFromDevice()){ requestPermission(); } mRecordLabel =(TextVIEw) findVIEwByID(R.ID.recordLbl); mRecordBtn =(button)findVIEwByID(R.ID.recordBtn); mfilename = Environment.getExternalStorageDirectory() + "/file.3gp"; mRecordBtn.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { if (checkPermissionFromDevice()) { if (event.getAction() == MotionEvent.ACTION_DOWN) { startRecording(); mRecordLabel.setText("Recording Started..."); } else if (event.getAction() == MotionEvent.ACTION_UP) { stopRecording(); mRecordLabel.setText("Recording Stoped..."); } } else{ requestPermission(); } return false; } }); uploadBtn =(button)findVIEwByID(R.ID.uplaodBtn); uploadBtn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { sendfile(); } }); } private voID startRecording() { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setoutputfile(mfilename); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() Failed"); } mRecorder.start(); } private voID stopRecording() { mRecorder.stop(); mRecorder.release(); mRecorder = null; } private voID requestPermission(){ ActivityCompat.requestPermissions(this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUdio, Manifest.permission.READ_EXTERNAL_STORAGE },REQUEST_PERMISSION_CODE); } @OverrIDe public voID onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch(requestCode) { case REQUEST_PERMISSION_CODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show(); else{ Toast.makeText(this, "Permission DenIEd", Toast.LENGTH_SHORT).show(); } } break; } } private boolean checkPermissionFromDevice(){ int write_internal_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); int record_audio_result =ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUdio); int read_internal_storage_result =ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE); return write_internal_storage_result == PackageManager.PERMISSION_GRANTED && record_audio_result == PackageManager.PERMISSION_GRANTED && read_internal_storage_result == PackageManager.PERMISSION_GRANTED; } private voID sendfile() { OkhttpClIEnt clIEnt = new OkhttpClIEnt(); file f = new file(mfilename); String content_type = getMimeType(f.getPath()); String file_path = f.getabsolutePath(); Requestbody file_body = Requestbody.create(MediaType.parse(content_type),f); Requestbody requestbody = new Multipartbody.Builder() .setType(Multipartbody.FORM) .addFormDataPart("Title", content_type) .addFormDataPart("uploaded_file",file_path.substring(file_path.lastIndexOf("/")+1), file_body) .build(); Request request = new Request.Builder() .url("http://192.168.8.100/etrack/save_audio.PHP") .post(requestbody) .build(); clIEnt.newCall(request).enqueue(new Callback() { @OverrIDe public voID onFailure(Call call, IOException e) { // Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show(); } @OverrIDe public voID onResponse(Call call, final Response response) throws IOException { // if (!response.isSuccessful()) { // Toast.makeText(MainActivity.this, "Response error", Toast.LENGTH_LONG).show(); // } } }); } private String getMimeType(String path){ String extension = MimeTypeMap.getfileExtensionFromUrl(path); return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); }}这是我的PHP代码:
<?PHP$file_path = "images/";$file_path = $file_path . basename($_fileS['uploaded_file']['name']);if(move_uploaded_file($_fileS['uploaded_file']['tmp_name'],$file_path)) { echo "success";}else { echo "error";}?>我的构建:gradle代码:
apply plugin: 'com.androID.application'androID { compileSdkVersion 25 defaultConfig { applicationID "www.teamruby.com.samplerecord" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionname "1.0" testInstrumentationRunner "androID.support.test.runner.AndroIDJUnitRunner" } buildTypes { release { MinifyEnabled false proguardfiles getDefaultProguardfile('proguard-androID.txt'), 'proguard-rules.pro' } } repositorIEs { maven { url "http://dl.bintray.com/lukaville/maven" } } productFlavors { }}dependencIEs { implementation filetree(include: ['*.jar'], dir: 'libs') implementation 'com.androID.support:appcompat-v7:24.2.' implementation 'com.androID.support:design:24.2.0' implementation 'com.androID.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androIDTestImplementation 'com.androID.support.test:runner:1.0.2' androIDTestImplementation 'com.androID.support.test.espresso:espresso-core:3.0.2' implementation 'com.squareup.okhttp3:okhttp:3.6.0'}解决方法:
file f = new file(mfilename);Multipartbody.Builder data = new Multipartbody.Builder(); data.setType(Multipartbody.FORM); data.addFormDataPart("uploaded_file", "file.3gp", Requestbody.create(MediaType.parse("media/type"), f)); Requestbody requestbody = data.build(); Request request = new Request.Builder() .url("192.168.8.100/etrack/save_audio.PHP").post(requestbody) .build(); 总结 以上是内存溢出为你收集整理的Android Studion错误Okhttp3单击按钮后未上传音频文件全部内容,希望文章能够帮你解决Android Studion错误Okhttp3单击按钮后未上传音频文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)