如何从图库中选择图像的目录和文件名 [英] How to get Directory and file name of an image selected from gallery
问题描述
我需要使用此功能,
java.io.File.File(文件目录,字符串名称)
java.io.File.File(File dir, String name)
公开文件(文件目录,字符串名称)
public File (File dir, String name)
在API等级1构造一个新的文件添加使用指定的
目录和名称。 DIR参数所在的目录文件
存储。命名该文件的名称。引发NullPointerException - 如果名称为
空。
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.?
下面是片段:
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?
我引用<一个href=\"http://chintankhetiya.word$p$pss.com/2013/07/18/sharing-text-image-in-twitter-android-example/\"相对=nofollow>共享文本,图像在Twitter的Android的例子。但我想用我自己的选择,而不是像他的URL形象一切都很好。请帮助。
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.
推荐答案
的 EXTRA_ALLOW_MULTIPLE
选择是通过 Intent.putExtra设置的意图( )
方法:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
以上您的code应该是这样的:
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);
注:EXTRA_ALLOW_MULTIPLE选项仅在Android的API 18和可用更高
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
}
}
这篇关于如何从图库中选择图像的目录和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!