Android gets the of URL example for the path to the selected image in the SD card

  • 2020-05-17 06:19:48
  • OfStack

Recently, I have been working on a picture uploading function, and I need to provide the path to upload the picture in SD card. I have seen some examples on the Internet, and successfully changed and debugged it. The code is very simple. The layout file is as follows:
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<Button 
android:id="@+id/select" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text=" Please select a SD Pictures in the card " 
/> 
</LinearLayout> 

The java file is as follows:
 
package com.lostinai; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

public class QueryPictureUrlActivity extends Activity { 
private Button select; 
private final String IMAGE_TYPE = "image/*"; 
private final int IMAGE_CODE = 0; 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
select = (Button)findViewById(R.id.select); 
select.setOnClickListener(new View.OnClickListener(){ 
public void onClick(View v) { 
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); 
getAlbum.setType(IMAGE_TYPE); 
startActivityForResult(getAlbum, IMAGE_CODE); 
} 
}); 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
if (resultCode != RESULT_OK) { // Here the  RESULT_OK  Is the system custom 1 A constant  
// Log.e(TAG,"ActivityResult resultCode error"); 
return; 
} 
Bitmap bm = null; 
ContentResolver resolver = getContentResolver(); 
if (requestCode == IMAGE_CODE) { 
try { 
Uri originalUri = data.getData(); // Acquired picture uri 
bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // Look to the bitmap The picture  
//  It starts here 2 Part, the path to get the image:  
String[] proj = {MediaStore.Images.Media.DATA}; 
Cursor cursor = managedQuery(originalUri, proj, null, null, null); 
// As I understand it   This is the index value to get the image selected by the user  
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
cursor.moveToFirst(); 
// Finally, get the image path according to the index value  
String path = cursor.getString(column_index); 
Log.e("Lostinai",path); 

}catch (IOException e) { 

Log.e("Lostinai",e.toString()); 

} 

} 
} 
} 

Don't forget to add permissions at the end
 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Related articles: