Bluetooth를 통해 파일을 전송하는 데 문제가 있습니다. 파일을 보내려고하면 전송이 '알 수없는 파일'오류로 '실패'로 표시됩니다. 내 파일 경로를 두 번 확인했지만 여전히이 문제가 있습니다. 너희들 내가 빠진 것을 볼 수 있니? 파일을 수신 할 것으로 예상되는 대상 전화에 사용자에게 수락 여부를 묻는 들어오는 파일 알림이 표시되지 않습니다. 나는 이것이 실패가있는 곳이라고 믿는다. 너희들은 '권한 요청'(나는 우리가 그렇게 부를 수있을 것 같은)을 목표 장치로 전달하는 법을 안다.수신자가 파일을 수락하지 않도록 사용자에게 묻습니다.
//some code used from
// http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/
// http://developer.android.com/guide/topics/connectivity/bluetooth.html
package com.project.BluetoothTransfer_v1000;
import java.io.File;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("DefaultLocale")
public class TransferFragment extends Fragment{
private TextView filePathTextView;
private Button startTransferButton;
private ImageView bluetoothImage;
ProgressDialog transferDialog;
Handler updateBarHandler;
private static final int REQUEST_BLUETOOTH = 1;
private static final int DISCOVER_DURATION = 300;
Context context;
ArrayAdapter mArrayAdapter;
long timeCheckStart = 0;
long timeCheckEnd = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//set the user interface layout for this activity
setRetainInstance(false);
View v = inflater.inflate(R.layout.activity_bluetooth_transfer, parent, false);
context = this.getActivity();
filePathTextView = (TextView) v.findViewById(R.id.file_path_textView);
startTransferButton = (Button) v.findViewById(R.id.start_transfer_button);
bluetoothImage = (ImageView) v.findViewById(R.id.bluetooth_imageView);
bluetoothImage.setClickable(true);
startTransferButton.setOnClickListener(new View.OnClickListener() {
//start transfer processes
@Override
public void onClick(View v){
//check to make sure the file path text view != null
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
int REQUEST_ENABLE_BT = -1;
//ensure the device being used has bluetooth capability
if (filePathTextView.getText().toString().length() > 4 && btAdapter != null){
//check-enable bluetooth
if (!btAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
//make the device discoverable and check to make sure device isn't already discoverable
if (timeCheckStart == 0 || System.currentTimeMillis() - 60000 > timeCheckStart){
timeCheckStart = System.currentTimeMillis();
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
startActivity(discoverableIntent);
}
// /storage/emulated/0/Test.jpg
// /storage/extSdCard/Test.jpg
String filePath = filePathTextView.getText().toString();
Toast.makeText(context, filePath, Toast.LENGTH_LONG);
String fileType = filePath.substring(filePath.length()-3,filePath.length()).toLowerCase();
//handles the sending of different file types
//@@@@@@@@@@@@@@@@@@ where im having trouble @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
switch (fileType){
case "jpg": //allow to fall through to png
case "png": Intent pictureIntent = new Intent(Intent.ACTION_SEND);
pictureIntent.setType("image/*");
pictureIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(pictureIntent, "Send Image"));
break;
case "mp3": Intent audioIntent = new Intent(Intent.ACTION_SEND);
audioIntent.setType("audio/*");
audioIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(audioIntent, "Send Audio"));
break;
case "txt": Intent textIntent = new Intent(Intent.ACTION_SEND);
textIntent.setType("text/*");
textIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(textIntent, "Send Text"));
break;
default: new AlertDialog.Builder(context).setTitle("Alert")
.setMessage("The file type submitted is not supported: ("+fileType+")")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show(); break;
}//end fileType switch
}//if text view null (if)
else {
//alert user to input file path
new AlertDialog.Builder(context).setTitle("Error")
.setMessage("Please insert a filename to send and be sure to include the type extension.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
}//bt equipped/text view null check (else)
}//end anon class
});
bluetoothImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//display dialog showing program specs and creators
new AlertDialog.Builder(context).setTitle("About")
.setMessage("Created by:"+"\n"+ "Hal, Chris, and Roger")
.setPositiveButton("Awesome!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
return v;
}
}