Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 接收方未要求用户接受文件_Android_Bluetooth - Fatal编程技术网

Android 接收方未要求用户接受文件

Android 接收方未要求用户接受文件,android,bluetooth,Android,Bluetooth,我在通过蓝牙发送文件时遇到一些困难。尝试发送文件后,它会将传输列为“失败”,并出现“未知文件”错误。我已经仔细检查了我的文件路径,但仍然有这个问题。你们看到我遗漏的东西了吗?本应接收文件的目标手机未显示要求用户接受或不接受文件的传入文件通知。我相信这就是失败的地方。你们知道如何将“权限询问”(我想我们可以这样称呼)传递到目标设备吗 //some code used from // http://examples.javacodegeeks.com/android/core/ui/progr

我在通过蓝牙发送文件时遇到一些困难。尝试发送文件后,它会将传输列为“失败”,并出现“未知文件”错误。我已经仔细检查了我的文件路径,但仍然有这个问题。你们看到我遗漏的东西了吗?本应接收文件的目标手机未显示要求用户接受或不接受文件的传入文件通知。我相信这就是失败的地方。你们知道如何将“权限询问”(我想我们可以这样称呼)传递到目标设备吗

//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;
    }
}

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

简而言之:您不能传递使用创建的URI

Uri=Uri.parse(字符串)

因为它缺少诸如
标题
MIME\u类型
之类的信息。 这就是文件类型未知(丢失)并引发此错误的原因

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);
希望这有帮助

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);