【问题标题】:Bluetooth File Transfer for my app我的应用程序的蓝牙文件传输
【发布时间】:2017-03-19 07:56:36
【问题描述】:

我正在尝试制作一个类似于共享但不那么高级的应用程序...主要的 activity.xml 有 5 个按钮,一个用于应用程序 2 用于图像 3 用于音频 4 用于视频 5 用于文档。 1 将在列表视图中获取设备上所有已安装的应用程序,然后 onclick 将打开本机内置蓝牙选择器以配对设备并发送文件
2(已经完成了一半的工作)将从图库中获取图像预览它在图像视图上放置一个按钮单击将打开本机蓝牙选择器以配对和发送文件
3 4 5 都会做类似的工作

问题出在按钮 2 java 代码中

public class MainImage extends Activity {
    private static int RESULT_LOAD_IMAGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_main);
        Button sendim = (Button) findViewById(R.id.imSend);
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);

            Bitmap bmp = null;
            try {
                bmp = getBitmapFromUri(selectedImage);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(); }
            imageView.setImageBitmap(bmp);
            Bitmap bm = 
        BitmapFactory.decodeResource(getResources(),bmp.getGenerationId());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[]b = baos.toByteArray();

        }
    }



    private Bitmap getBitmapFromUri(Uri uri) throws IOException {
        ParcelFileDescriptor parcelFileDescriptor =
                getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
    }

    public void sendx(View v) {

     }
}

如何将bmp的字节流baos发送到sendx方法通过蓝牙发送

这里是帮助我完成它的文件建议一些东西 https://github.com/avd10/bluetoothfiletranferAndroid

【问题讨论】:

  • 请不要链接到 Github,而是请在您的代码中发布minimal reproducible example 问题。我们不是在你自己的代码上实现一个完整的功能
  • 完成.. stackoverflow 的新手
  • 您说“按钮 2”是您的问题,但您的代码中是什么?它到底有什么问题?
  • 你需要sendx吗?是在 XML 中设置的吗?如果是这样,如果您在 Java 代码中设置了点击侦听器会有所帮助吗?
  • @cricket_007 sendx 方法在 xml 中设置 android:onclick........ android:onclick 和 setonclick 监听器都将用于相同的目的......

标签: android bluetooth


【解决方案1】:

如何将bmp的字节流baos发送到sendx方法通过蓝牙发送

你把它作为类的成员变量存储怎么样?

public class MainImage extends Activity {
    private static int RESULT_LOAD_IMAGE = 1;

    private Bitmap selectedBitmap = null;
    private ImageView imageView;

    public void sendx(View v) {
        byte[] toSend = bitmapToBytes(selectedBitmap);
        if (toSend.length != 0) {
            // TODO: Send to bluetooth
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_main);
        imageView = (ImageView) findViewById(R.id.imgView);
        Button sendim = (Button) findViewById(R.id.imSend);
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            selectedImage = data.getData();

            ...

            try {
                selectedBitmap = getBitmapFromUri(selectedImage);
                imageView.setImageBitmap(selectedBitmap);
                Log.d("ImageLoader", "image has been loaded!");
            } catch (IOException e) {
                e.printStackTrace(); 
            }
        } // end request code
    } // end onActivityResult

    private byte[] bitmapToBytes(Bitmap bmp) {
        if (bmp == null) return new byte[]; // empty array
        Bitmap bm = BitmapFactory.decodeResource(getResources(),bmp.getGenerationId());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2012-04-02
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多