我正在尝试使应用程序类似于共享它但不是那么高级...主要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
答案 0 :(得分:0)
如何将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();
}