Android 获取要上载到服务器的文件路径
我正在创建一个应用程序,我想在其中将图像发送到web服务器。为此,我使用了camera和gallery intent。现在我想获得用户选择的图像路径,以便我可以上载到服务器。现在如何获得路径 代码Android 获取要上载到服务器的文件路径,android,file-upload,Android,File Upload,我正在创建一个应用程序,我想在其中将图像发送到web服务器。为此,我使用了camera和gallery intent。现在我想获得用户选择的图像路径,以便我可以上载到服务器。现在如何获得路径 代码 您好,您可以尝试下面的代码从库中获取所选图像的路径 要打开图库 private static int RESULT_LOAD_IMAGE = 1; //variable declared globally 打开按钮ClickListener() Intent i = new Intent(Inten
您好,您可以尝试下面的代码从库中获取所选图像的路径 要打开图库
private static int RESULT_LOAD_IMAGE = 1; //variable declared globally
打开按钮ClickListener()
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
活动结果:
if(data != null){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex); //path of image...
cursor.close();
}
试试这种方法,希望这能帮助你解决问题。 活动\u main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/select_photo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Gallery"/>
<Button
android:id="@+id/take_photo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Camera"/>
</LinearLayout>
</LinearLayout>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
注意:请不要忘记AndroidManifest.xml中的此权限
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/select_photo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Gallery"/>
<Button
android:id="@+id/take_photo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Camera"/>
</LinearLayout>
</LinearLayout>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您的解决方案可能会起作用,但对于我的情况,在Android 6.0(API 23)Nexus 5上,它不起作用:URI:
content://com.android.externalstorage.documents/document/primary%3APictures%2F123abc.jpg
Method:getAbsolutePath
returnnull在您的情况下,它仍然无法获取绝对路径。