How to send image through bluetooth in android? - android

In my application, I open my gallery and pick a picture and then show my image in an image view then I'm supposed to send it through bluetooth.
That's what i've reached :
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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
iv.setImageBitmap(yourSelectedImage);
How to send it through bluetooth?
N.B I've opened my bluetooth and made it also discover-able by other devices .
Please I'll be thankful if u helped me in my assignment =]

Related

Importing images into DataBase

in my app I use SqLite Database "Templates", where I have table "favourites", constains of four columns "id", "description", "imagePath", "category". I would like user to be able use their own images in my app, like this:
When user click button, he choose image from gallery, write description and choose category of image. I know I need use Content Values to do this. But how can I do my table has imagePathes, i.e how can I show them later using RecyclerView and Picasso?
You can load image from device memory:
Picasso.get().load(new File("path-to-you-image/image.png")).into(imageView);
If you are asking "how to get image path" - you can get it in the onActivityResult method:
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();

cursor.getString() returning null

I am new to android (started this week) and am currently writing a photo album app for a class. I am currently trying to get an image selected by the user, however, after selecting the image I am using cursor to get the path but the getString() method is returning null. Here is the code segment in onActivityResult:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();
//create photo object
I used the debugger tool to step through the code. data.getData() does not return null.filePathColumn[0] contains "_data", crusor.moveToFirst() returns true and columnIndex = 0. Then the cursore.getString(columnIndex) returns null. Can anybody help me figure out what is going wrong ?

Android email app with attachment not working

I'm trying to develop email app and I want to attach a image to it, but I'm getting NullPointerException. Here's my code:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentfile = cursor.getString(columnIndex);
Log.e("Attachment Path: ", attachmentfile);
URI = Uri.parse("file://" + attachmentfile);
cursor.close();`
Please help me
I think your problem is that you are not putting right the file path.
The following works for me:
Uri.parse("file:///"+attachmentfile));
Note that file path has 3 "/", the first two are for the "file://" header, the other because sdcard dir is inside the root directory of the filesystem, which is "/" in linux.

How to get a File from a URI?

I select an image from the image gallery in my app. In the onActivityResult it returns an Intent object and I execute the getData() method on it. I get the image URI back. I am now trying to convert that in to a File so I can send it to AWS S3.
This is what I have been trying but it just returns null:
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
I also try
File file = new File(imageURI.getPath())
also null. Is this impossible or can somebody help?
supposing your onActivityResult checks in with the Intent object being ReturnedIntent you can do
File f = new File(ReturnedIntent.getData());
or get an inputstream and use it
getContentResolver().openInputStream(ReturnedIntent.getData())
//returns inputstream
what is the essence of this String object String filePath ?

How to change file URI string

How to change something file:///system/media/lockscreen/lockscreen_001.jpg to
something like /mnt/sdcard/myPicture.jpg
The reason why I want to change is that file:/// is wrong if I want to further process. It is hard to tell but if I get the URI from Uri uri= data.getData(); is file:///system/media/lockscreen/lockscreen_001.jpg, how to handle because normally is start with mnt
Try this :
Uri uri = Uri.parse("file:///system/media/lockscreen/lockscreen_001.jpg");
Toast.makeText(getApplicationContext(), ""+uri.getPath(), Toast.LENGTH_LONG).show();
uri.getPath() will give you the path by eliminating file: extension.
EDIT :
Uri urinew = Uri.parse(uri.getPath());
This will give you new uri with the path you got.
Hope it helps you.
Thanks.
Hope this code can help you :
Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);

Categories

Resources