2017-03-19 37 views
0

我想讓應用程序類似於共享它,但不是如此先進......主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的send to sendx方法通過blueooth發送

這裏是文件幫我完成它表明的東西 https://github.com/avd10/bluetoothfiletranferAndroid

+0

請不要鏈接到Github,而是請在您的代碼中發佈問題的[mcve]。我們不是在這裏實現你自己的代碼 –

+0

完成一個完整的功能..新的到stackoverflow –

+0

你說「按鈕2」是你的問題,但在你的代碼是什麼?它究竟有什麼問題? –

回答

0

如何發送BMP的字節流BAOS到sendx方法通過blueooth

送它你怎麼樣它存儲爲類的成員變量?

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