我想使用蓝牙制作文件传输应用程序,但是选择文件时遇到问题,即所选文件的地址没有出现在我的主活动中。如何获取文件路径?
我已经使用了获取和设置文本,但是我的文件路径未出现在“主要活动”中
StorageFragment.java
File root;
private List<FileInfo> mCurFileInfoList;
private String mCurPath;
private FileItemAdapter mFileItemAdapter;
private PathItemAdapter mFilePathAdapter;
private ProgressDialog dialog;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurFileInfoList = new ArrayList<>();
root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
mCurPath = Environment.getExternalStorageDirectory().getAbsolutePath();
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
loadPathInfo(mCurPath);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
requestPermissions(new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
}
}
/**
* Method request Permission
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_READ_EXTERNAL_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadPathInfo(mCurPath);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_storage, null);
ButterKnife.bind(this, rootView);
mPathListView.setLayoutManager(new LinearLayoutManager(getContext(), HORIZONTAL, false));
mPathListView.setAdapter(mFilePathAdapter = new PathItemAdapter(getActivity(), this));
mStorageListView.setLayoutManager(new LinearLayoutManager(getContext()));
mStorageListView.setAdapter(mFileItemAdapter = new FileItemAdapter(getActivity(), this, this));
mSwipeRefresh.setOnRefreshListener(this);
return rootView;
}
private void loadPathInfo(final String path) {
mCurFileInfoList.clear();
Observable.create(new ObservableOnSubscribe<List<FileInfo>>() {
@Override
public void subscribe(ObservableEmitter<List<FileInfo>> e) throws Exception {
mCurFileInfoList = FileUtils.getInfoListFromPath(path);
e.onNext(mCurFileInfoList);
e.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<FileInfo>>() {
@Override
public void accept(List<FileInfo> fileInfos) throws Exception {
mFileItemAdapter.setDataList(mCurFileInfoList);
mSwipeRefresh.setRefreshing(false);
mCurPath = path;
mFilePathAdapter.setPathView(mCurPath);
mStorageListView.smoothScrollToPosition(0);
mPathListView.scrollToPosition(mFilePathAdapter.getItemCount() - 1);
}
});
}
@Override
public void onRefresh() {
loadPathInfo(mCurPath);
}
/**
* Methode to select file
* @param v
*/
@Override
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof String) {
loadPathInfo((String) tag);
} else if (tag instanceof FileInfo) {
FileInfo info = (FileInfo) tag;
if (info.isFolder()) {
loadPathInfo(info.getFilePath());
} else{
getPathFile(root);
}
}
}
public void getPathFile(File f) {
root = f;
TextView pathFile = (TextView) mPathListView.findViewById(R.id.path_list_text);
pathFile.setText(f.getAbsolutePath());
Intent sendPath = new Intent(this.getActivity(), mainbluetooth.class);
sendPath.putExtra("pathId", "StorageFragment");
sendPath.putExtra("1", pathFile.getText().toString());
startActivityForResult(sendPath, 1990);
}
mainbluetooth.java
public class mainbluetooth extends AppCompatActivity {
@BindView(R.id.file_path)
EditText dataPath;
//Create Objects-------------------------------------------------------
public static final int FILE_REQUEST_CODE = 10;
private static final int DISCOVER_DURATION = 300;
private static final int REQUEST_BLU = 1;
BluetoothAdapter btAdatper = BluetoothAdapter.getDefaultAdapter();
//---------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainbluetooth);
ButterKnife.bind(this);
}
@OnClick(R.id.Sendblue)
public void sendViaBluetooth() {
if (!dataPath.equals(null)) {
if (btAdatper == null) {
Toast.makeText(this, "Device not support bluetooth", Toast.LENGTH_LONG).show();
} else {
enableBluetooth();
}
} else {
Toast.makeText(this, "Please select a file.", Toast.LENGTH_LONG).show();
}
}
@OnClick(R.id.select_file)
public void btnSelectFile() {
Intent intent = new Intent(this, StorageActivity.class);
startActivityForResult(intent, FILE_REQUEST_CODE);
}
//exit to application---------------------------------------------------------------------------
@OnClick(R.id.buttonQuit)
public void Exit() {
btAdatper.disable();
Toast.makeText(this, "*** Now Bluetooth is off... Thanks. ***", Toast.LENGTH_LONG).show();
finish();
}
//Method for send file via bluetooth------------------------------------------------------------
public void enableBluetooth() {
Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);
startActivityForResult(discoveryIntent, REQUEST_BLU);
}
//Override method for sending data via bluetooth availability--------------------------
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (resultCode == FILE_REQUEST_CODE) {
try {
final String sender =this.getIntent().getExtras().getString("SENDER_KEY");
if (sender!=null){
Intent recievePath = getIntent();
String pathSelectFile = recievePath.getStringExtra("1");
dataPath.setText(pathSelectFile);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (resultCode == DISCOVER_DURATION && requestCode == REQUEST_BLU) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("*/*");
File file = new File(dataPath.getText().toString());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(i, 0);
if (list.size() > 0) {
String packageName = null;
String className = null;
boolean found = false;
for (ResolveInfo info : list) {
packageName = info.activityInfo.packageName;
if (packageName.equals("com.android.bluetooth")) {
className = info.activityInfo.name;
found = true;
break;
}
}
//CHECK BLUETOOTH available or not------------------------------------------------
if (!found) {
Toast.makeText(this, "Bluetooth not been found", Toast.LENGTH_LONG).show();
} else {
i.setClassName(packageName, className);
startActivity(i);
}
}
} else {
Toast.makeText(this, "Bluetooth is cancelled", Toast.LENGTH_LONG).show();
}
}
}
}