接收方不要求用户接受文件

时间:2014-04-17 21:58:06

标签: android 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;
    }
}

1 个答案:

答案 0 :(得分:0)

也许这个答案可以帮到你:

https://stackoverflow.com/a/19618432/3743093

简而言之:您无法传递使用

创建的URI

Uri uri = Uri.parse(String);

,如

pictureIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));

因为它缺少TITLEMIME_TYPE等信息。 这就是为什么文件类型未知(缺失)并抛出此错误的原因。

ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/*");
Uri uri = getContentResolver().insert(Uri.parse(filename),
        values);
pictureIntent.putExtra(Intent.EXTRA_STREAM, uri);

希望这有帮助。