我正在从图库中挑选文件并将拾取的文件上传到服务器。如何将uri转换为inputStream数据并将流数据上传到服务器?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data != null) {
//no data present
Uri uri = data.getData();
String filePath = data.getData().getPath();
// String path = uri.getPath();
file = new File(filePath);
String name = getContentName(getContentResolver(), uri);
try {
InputStream inStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
attachFile.addView(imageView);
TextView textView = new TextView(this);
textView.setText(name);
attachFile.addView(textView);
return;
}
}
}
我使用意图
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addFlags(ST)
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
我的问题是URI数据转换InputStream的,但我使用该文件的InputStream离子库不能被上传到服务器poicked文件。 如何将inputStream转换为outputstream以保存在getCacheDir()中我引用此网站How to get the file path for the picked files from the external storage in android?
请帮助我如何将InputStream数据上载到离子库中。
使用'的InputStream inStream'阅读,现在你不使用它 – pskink
@ pskink我不知道如何使用InputStream的..如果你知道方法请帮我 –
https://developer.android请参阅'read()'方法(当然这两个方法是'byte [] b''是你应该使用的变体) – pskink