问题
I need to use this function,
java.io.File.File(File dir, String name)
public File (File dir, String name)
Added in API level 1 Constructs a new file using the specified directory and name. Parameters dir the directory where the file is stored. name the file's name. Throws NullPointerException if name is null.
Now as we can see it needs file dir and name as parameters. Now I am selecting an Image from gallery by intent. How can I get the dir and name for the selected file.?
Here is the snippet:
public File String_to_File(String img_url) {
try {
File rootSdDirectory = Environment.getExternalStorageDirectory();
---------------------------------------------------------------------------------------
**casted_image = new File(rootSdDirectory, "attachment.jpg");**
---------------------------------------------------------------------------------------
if (casted_image.exists()) {
casted_image.delete();
}
casted_image.createNewFile();
FileOutputStream fos = new FileOutputStream(casted_image);
URL url = new URL(img_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
InputStream in = connection.getInputStream();
byte [ ] buffer = new byte [1024];
int size = 0;
while ((size = in.read(buffer)) > 0) {
fos.write(buffer, 0, size);
}
fos.close();
return casted_image;
} catch (Exception e) {
System.out.print(e);
// e.printStackTrace();
}
return casted_image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
iv.setImageURI(selectedImage);
// String picturePath contains the path of selected Image
}
}
What should be done to pass proper parameters to the function in between the lines?
I am referencing sharing-text-image-in-twitter-android-example . All is good except I want to use my own selected image instead of his URL image. Please help.
回答1:
The EXTRA_ALLOW_MULTIPLE
option is set on the intent through the Intent.putExtra()
method:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Your code above should look like this:
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE );
//intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Note: the EXTRA_ALLOW_MULTIPLE option is only available in Android API 18 and higher.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
casted_image = new File(picturePath);
cursor.close();
// String picturePath contains the path of selected Image
}
}
回答2:
You can do like this.
File selectedFile = Environment.getExternalStorageDirectory();
File parentDir = selectedFile.getParentFile();
casted_image = new File(parentDir, "attachment.jpg");
or if you want to pass selected file's name instead of specific filename. use following line.
casted_image = new File(parentDir, selectedFile.getName());
you can also check whether selected file is directory or a ordinary file, by using selectedFile.isFile();
& selectedFile.isDirectory();
来源:https://stackoverflow.com/questions/25760676/how-to-get-directory-and-file-name-of-an-image-selected-from-gallery