我正在开发一个简单的应用程序,用户可以将其文档上传到服务器。我能够将文件上传到服务器,但我得到的问题是,当我从这个位置/storage/sdcard/sample.txt
选择文件时,我能够上传,但是当我从不同位置选择文件时,文件没有上传在logcat我得到这个位置/storage/sdcard/demo.docx
,“demo.docx”是我从不同位置选择的文件
MyLogcat
04-20 18:31:45.990 1404-2066/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
04-20 18:31:45.995 1404-1432/? W/IntentResolver: resolveIntent: multiple matches, only some with CATEGORY_DEFAULT
04-20 18:31:45.995 1404-1432/? I/ActivityManager: START u0 {act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] typ=*/* cmp=com.android.documentsui/.DocumentsActivity} from uid 10217 on display 0
04-20 18:31:45.997 1404-1432/? V/WindowManager: addAppToken: AppWindowToken{12932f9c token=Token{1cf9a30f ActivityRecord{1b7ac06e u0 com.android.documentsui/.DocumentsActivity t2252}}} to stack=1 task=2252 at 1
04-20 18:31:46.081 1404-4882/? V/WindowManager: Adding window Window{1596757a u0 com.android.documentsui/com.android.documentsui.DocumentsActivity} at 3 of 6 (after Window{e2669c0 u0 org.pitechnologies.pi_technologies1/org.pitechnologies.pi_technologies1.FilePik})
04-20 18:31:46.124 2844-2862/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-20 18:31:46.125 2844-2862/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5e0b5a0, error=EGL_SUCCESS
04-20 18:31:46.202 1404-1431/? W/IntentResolver: resolveIntent: multiple matches, only some with CATEGORY_DEFAULT
04-20 18:31:46.210 1404-1559/? I/ActivityManager: Displayed com.android.documentsui/.DocumentsActivity: +185ms
04-20 18:31:46.240 5054-5069/? D/OpenGLRenderer: endAllStagingAnimators on 0xb41c6b80 (RippleDrawable) with handle 0xb43da890
04-20 18:31:46.259 2844-2844/? W/ResourceType: No package identifier when getting value for resource number 0x00000000
04-20 18:31:46.259 2844-2844/? W/PackageManager: Failure retrieving resources for com.android.music: Resource ID #0x0
04-20 18:31:46.285 2844-2844/? D/Documents: Current stack:
04-20 18:31:46.285 2844-2844/? D/Documents: * Root{authority=com.android.providers.downloads.documents, rootId=downloads, title=Downloads}
04-20 18:31:46.285 2844-2844/? D/Documents: +-- Document{docId=downloads, name=null}
04-20 18:31:46.292 2844-4869/? D/Documents: userMode=0, userSortOrder=0 --> mode=1, sortOrder=2
04-20 18:31:46.300 2844-2844/? W/ResourceType: No package identifier when getting value for resource number 0x00000000
04-20 18:31:46.300 2844-2844/? W/PackageManager: Failure retrieving resources for com.android.music: Resource ID #0x0
04-20 18:31:46.336 2844-4869/? W/Documents: Failed to load thumbnail for content://com.android.providers.downloads.documents/document/2: java.io.FileNotFoundException: No such file or directory
04-20 18:31:48.897 1404-2066/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
04-20 18:31:48.908 2844-2844/? D/Documents: onFinished() [content://com.android.providers.downloads.documents/document/4]
04-20 18:31:48.939 5054-5054/? I/tagconvertstr-->: [demo.docx]
04-20 18:31:48.963 5054-5069/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-20 18:31:48.963 5054-5069/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa50e52e0, error=EGL_SUCCESS
04-20 18:31:49.390 2844-2844/? I/art: Explicit concurrent mark sweep GC freed 3193(245KB) AllocSpace objects, 2(40KB) LOS objects, 30% free, 1142KB/1654KB, paused 9.317ms total 17.764ms
04-20 18:31:49.470 2844-2844/? I/art: Explicit concurrent mark sweep GC freed 595(68KB) AllocSpace objects, 2(40KB) LOS objects, 33% free, 1033KB/1545KB, paused 1.860ms total 24.646ms
04-20 18:31:49.490 2844-2844/? I/art: Explicit concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 33% free, 1033KB/1545KB, paused 1.842ms total 19.211ms
04-20 18:31:49.493 2844-2844/? E/StrictMode: class com.android.documentsui.DocumentsActivity; instances=2; limit=1
android.os.StrictMode$InstanceCountViolation: class com.android.documentsui.DocumentsActivity; instances=2; limit=1
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
04-20 18:31:49.984 2094-2108/? I/art: Background sticky concurrent mark sweep GC freed 32252(1965KB) AllocSpace objects, 0(0B) LOS objects, 21% free, 7MB/9MB, paused 10.375ms total 29.108ms
04-20 18:31:50.787 1404-2066/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
04-20 18:31:50.806 5054-5089/? W/System.err: java.io.FileNotFoundException: /storage/sdcard/demo.docx: open failed: ENOENT (No such file or directory)
04-20 18:31:50.806 5054-5089/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
04-20 18:31:50.806 5054-5089/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:78)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.james.mime4j.message.MessageWriter.writeBody(MessageWriter.java:76)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:202)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:223)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:179)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:102)
04-20 18:31:50.806 5054-5089/? W/System.err: at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:167)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:277)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:242)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:433)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.pitechnologies.pi_technologies1.FilePik$PostDataAsyncTask.doInBackground(FilePik.java:128)
04-20 18:31:50.807 5054-5089/? W/System.err: at org.pitechnologies.pi_technologies1.FilePik$PostDataAsyncTask.doInBackground(FilePik.java:107)
04-20 18:31:50.807 5054-5089/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
04-20 18:31:50.807 5054-5089/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-20 18:31:50.807 5054-5089/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-20 18:31:50.807 5054-5089/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-20 18:31:50.807 5054-5089/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-20 18:31:50.807 5054-5089/? W/System.err: at java.lang.Thread.run(Thread.java:818)
04-20 18:31:50.807 5054-5089/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-20 18:31:50.807 5054-5089/? W/System.err: at libcore.io.Posix.open(Native Method)
04-20 18:31:50.807 5054-5089/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-20 18:31:50.808 5054-5089/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
04-20 18:31:50.808 5054-5089/? W/System.err: ... 23 more
MainActivity
public class FilePik extends Activity {
Button b1,b2;
String displayName;
TextView txt_file_name_1;
private static final int PICKFILE_RESULT_CODE = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filepik);
txt_file_name_1 = (TextView)findViewById(R.id.textt);
b1 = (Button)findViewById(R.id.button12);
b2 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostDataAsyncTask().execute();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
}
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, PICKFILE_RESULT_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK ) {
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
displayName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = this.getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.i("tagconvertstr-->", "[" + displayName + "]");
}
} finally {
cursor.close();
}
}else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
Log.i("tagconvertstr", "[" + displayName + "]");
}
txt_file_name_1.setText(displayName);
}
// txt_file_name_1.setText(displayName);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://*****/*****/newfile.php");
File file1 = Environment.getExternalStorageDirectory();
File myDir = new File(file1, displayName);
FileBody fileBody1 = new FileBody(myDir);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file1", fileBody1);
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
final String responseStr = EntityUtils.toString(resEntity)
.trim();
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Log.e("", "RESULT : " + result);
}
}
}
答案 0 :(得分:0)
你应该使用文件的路径而不是他的名字。而不是
displayName = myFile.getName();
在此处使用getPath()
:
else if (uriString.startsWith("file://")) {
displayName = myFile.getPath();
Log.i("tagconvertstr", "[" + displayName + "]");
}
答案 1 :(得分:0)
您正在尝试访问外部SD卡中的文件,而它位于内部存储中。使用以下代码从内部存储中获取文件:
DELIMITER ;;
CREATE TRIGGER foo_bar_insert_type_check
BEFORE INSERT ON foo_type_bar
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'bar' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_bar';
END IF;
END;;
CREATE TRIGGER foo_bar_update_type_check
BEFORE UPDATE ON foo_type_bar
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'bar' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_bar';
END IF;
END;;
CREATE TRIGGER foo_baz_insert_type_check
BEFORE INSERT ON foo_type_baz
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'baz' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_baz';
END IF;
END;;
CREATE TRIGGER foo_baz_update_type_check
BEFORE UPDATE ON foo_type_baz
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'baz' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_baz';
END IF;
END;;
CREATE TRIGGER foo_buz_insert_type_check
BEFORE INSERT ON foo_type_buz
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'buz' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_buz';
END IF;
END;;
CREATE TRIGGER foo_buz_update_type_check
BEFORE UPDATE ON foo_type_buz
FOR EACH ROW
BEGIN
IF NEW.foo_type != 'buz' THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_type_buz';
END IF;
END;;
CREATE TRIGGER foo_whatever_insert_type_check
BEFORE INSERT ON foo_whatever
FOR EACH ROW
BEGIN
IF NEW.foo_type NOT IN ('baz', 'buz') THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_whatever';
END IF;
END;;
CREATE TRIGGER foo_whatever_update_type_check
BEFORE UPDATE ON foo_whatever
FOR EACH ROW
BEGIN
IF NEW.foo_type NOT IN ('baz', 'buz') THEN
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT = 'Invalid foo_type in foo_whatever';
END IF;
END;;
DELIMITER ;
答案 2 :(得分:-1)
你必须在你的情况下设置特定的mime类型Documnent Type才能工作