我需要使用这个功能,
java.io.File.File(File dir,String name)
公共文件(文件目录,字符串名称)
在API级别1中添加使用指定的构造新文件 目录和名称。参数dir文件所在的目录 存储。命名文件的名称。如果name为,则抛出NullPointerException 空。
现在我们可以看到它需要文件目录和名称作为参数。现在我按意图从画廊中选择一个图像。如何获取所选文件的目录和名称。?
以下是摘录:
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
}
}
如何将适当的参数传递给线之间的函数?
我引用了sharing-text-image-in-twitter-android-example。一切都很好,除了我想使用我自己选择的图像而不是他的URL图像。请帮忙。
答案 0 :(得分:1)
你可以这样做。
File selectedFile = Environment.getExternalStorageDirectory();
File parentDir = selectedFile.getParentFile();
casted_image = new File(parentDir, "attachment.jpg");
或者如果您想传递所选文件的名称而不是特定文件名。使用以下行。
casted_image = new File(parentDir, selectedFile.getName());
您还可以使用selectedFile.isFile();
检查所选文件是目录还是普通文件
&安培; selectedFile.isDirectory();
答案 1 :(得分:1)
通过EXTRA_ALLOW_MULTIPLE
方法在意图上设置Intent.putExtra()
选项:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
上面的代码应如下所示:
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及更高版本。
@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
}
}