我正在研究项目,因为我必须上传PDF文件或DOC文件,但是我写了太多代码但没有成功 每当我单击“提交吐司”时,说“上传PDF文件并退出活动”。 如何解决问题?
这是我使用的代码
public class contactus extends AppCompatActivity {
EditText name,email,mobile;
Button submit,uploadCv;
public static final String pdfurl = "https://umsctest.000webhostapp.com/testdb/upload.php";
Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactus);
name = findViewById(R.id.et_name);
email = findViewById(R.id.et_email);
mobile = findViewById(R.id.et_no);
submit = findViewById(R.id.bt_submit);
uploadCv = findViewById(R.id.bt_uploadCv);
InputFilter[] inputFilter = new InputFilter[1];
inputFilter[0] = new InputFilter.LengthFilter(10);
mobile.setFilters(inputFilter);
uploadCv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadfile();
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fname = name.getText().toString();
String mail = email.getText().toString();
String mob = mobile.getText().toString();
String filepath = FilePath.getPath(contactus.this,uri);
if (filepath == null)
{
Toast.makeText(getApplicationContext(),"Please Upload PDF file",Toast.LENGTH_SHORT).show();
}else
{
String pdfId = UUID.randomUUID().toString();
try {
new MultipartUploadRequest(contactus.this,pdfId,pdfurl).addFileToUpload(filepath, "pdf")
.addParameter("name",fname)
.addParameter("email",mail)
.addParameter("mobile",mob)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(5)
.startUpload();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"Error in Uploading Fis",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (MalformedURLException e) {
Toast.makeText(getApplicationContext(),"Error in Uploading File",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
if (fname.length() <=0)
{
name.setError("Name Please");
}else if (mail.length() <=0)
{
email.setError("Email Please");
}else if (mob.length() <=0)
{
mobile.setError("WhatsApp No. Please");
}else if (mob.length() >10)
{
mobile.setError("Only 10 Digits No.");
}else
{
name.setText("");
email.setText("");
mobile.setText("");
}
}
});
}
private void uploadfile() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent,"Select Pdf"),1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data !=null)
{
Uri path = data.getData();
uploadCv.setText("PDF Selected");
}
}
这是我的contactus(mainactivity).java
我也使用了FilePath.java
public class FilePath {
public static String getPath(final Context context, final Uri uri)
{
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri))
{
if (isExternalStorageDocument(uri))
{
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
}else if (isDownloadsDocument(uri))
{
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}else if (isMediaDocument(uri))
{
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type))
{
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}else if("video".equals(type))
{
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}else if ("audio".equals(type))
{
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {split[1]};
}else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
return null;
}
private static String getDataColumn(Context context, Uri contentUri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(contentUri, projection, selection, selectionArgs,null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri)
{
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
帮我找出它的返回空路径