안드로이드 앱을 만들고있는데
구글링해보니 이미지 파일 혹은 미디어 파일들의 절대경로를 얻는 방법만 있는데
제가 구현하고자하는 앱은 엑셀파일이나 워드파일 같은것을 서버에 업로드해서 앱 이용자들이 그것을 다운받는 앱을 만드려고 하거든요.. 혹시 도움주실분계실까요
제가 시도한 코드는 아래와 같습니다.
upload_btn_upload2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
upload2(file_path);
Toast.makeText(Upload2.this,"업로드 완료", Toast.LENGTH_SHORT).show();
finish();
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == FILE_LIST) {
check_box.setChecked(true);
file_path = getPathFromUri3(data.getData());
File f = new File(file_path);
text_file_name.setText(f.getName().toString());
}
}
public String getPathFromUri3(Uri uri){
String[] proj = {MediaStore.Files.FileColumns.DATA};// 이부분이 의문입니다 ㅠㅠ
CursorLoader cursorLoader = new CursorLoader(this, uri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
cursor.moveToFirst();
return cursor.getString(index);
}
private void upload2(String uri){
StorageReference storageRef = storage.getReferenceFromUrl("gs://mopt2-5e36d.appspot.com");
final Uri file = Uri.fromFile(new File(uri));
StorageReference riversRef = storageRef.child("files/"+file.getLastPathSegment());
UploadTask uploadTask = riversRef.putFile(file);
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
FileDTO fileDTO = new FileDTO();
fileDTO.fileUrl = downloadUrl.toString();
fileDTO.fileName = text_name.getText().toString();
fileDTO.description = text_description.getText().toString();
// 이거 다시 확인
fileDTO.userId = auth.getCurrentUser().getEmail();
database.getReference().child("files").push().setValue(fileDTO);
}
});
}
}
참고로 파이어베이스 이용중입니다 도움주시면 정말 감사하겠습니다!!