I have enabled My App for the share via options in the HTML page. Now, I am trying to store the HTML page from where this was triggered in my sdcard. For this purpose, I was trying to use the intent contents.
I used the following for images and It worked like charm..
Uri imageUri = (Uri) shareintent.getParcelableExtra(Intent.EXTRA_STREAM);
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
ContentResolver cr = getContentResolver();
InputStream is = cr.openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap imageBitmap = BitmapFactory.decodeStream(is ,null, options);
Please can you give me an hint of how to save the HTML page in my sdcard?
within a WebView (a layout which allows opening webpages within your application...among other things) and using HttpClient.execute() or HttpClient.get() should do the trick for you.
Related
Scenario: I am using MessagingStyle notification in a chat application. For image messages, I am using setData function to create image notification in conversation style.
public Message setData(String dataMimeType, Uri dataUri)
This method required dataUri but I have URL of image so I have created a bitmap from image url and then create a uri from batmap using function below.
public static Uri getImageUri(Context applicationContext, Bitmap photo) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), photo, "IMG_" + Calendar.getInstance().getTime(), null);
if (path == null) {
return null;
}
return Uri.parse(path);
} catch (SecurityException ex) {
ex.printStackTrace();
return null;
}
}
Problem: Now the problem is that when user open image gallery, these notifications images are showing because I used mediastore to create URI. I don’t want these image to show in gallery.
Limitation: I am also saving that URI in case If I have to generate this notification again. So I can’t delete that image immediately to avoid showing in gallery.
Question: Is there any way to save image privately in external storage where gallery don’t have access. But notification manager can access?
Is there any way to create Uri form url without downloading it? I have tried this but it also not working
url = new URL(messageNotification.getAttachmentImage());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
imageUri= Uri.parse(uri.toString());
Another workaround to show image in notification by using Messagings style like conversation as in WhatsApp
Download your image directly from url to a file in getExternalFilesDir(). Dont use an intermediate bitmap.
The MediaStore has no access to your getExternalFilesDir() so Gallery apps dont know about your files there.
The user can still use the Files app to see those images.
Then use a FileProvider to serve your files. Construct an uri with FileProvider.getUriForFile().
store your bitmap as File in some app-specific directory (not gallery) and make URI from file path. be aware of Scoped Storage and pick proper place for storing Bitmaps - check out in HERE, probably getFilesDir()/getCacheDir() method will be the best option
also worth trying may be this line:
Uri uri = Uri.parse(messageNotification.getAttachmentImage());
(encodedImageUrl = URLEncoder.encode(imageUrl, "utf-8") may be needed)
So I'm a beginner in android and I've been trying to experiment media related elements of Android. So far, I was able to display video, but I saw Viber and some other similar apps have some slick way of displaying video like below, wherein when tapped will open an app viewer wherein images/videos of app can be seen there:
Here is my code, it starts video immediately... I tried the .seekTo(100), ..seekTo(0) option but both gives me a black screen.
Uri uri = Uri.parse(mVideoFileName);
mVideoMessageView.setVideoURI(uri);
mediaC.setAnchorView(mVideoMessageView);
mVideoMessageView.start();
I wonder, how they made an overlay of a play button on the video, duration, and even timestamp and have a snapshot of it. Any advice would greatly be appreciated.
Thanks a lot.
int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);
Also you can use Glide for this
Glide
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( imageViewGifAsBitmap );
I’m using some big pictures that are stored in External Memory. I decode them, resize them and set the small bitmaps to various imageviews.
I keep track of those large images by storing their Uri’s. When the app stops, I convert Uri to paths and save them as Strings is a SQLite base:.
File myFile = new File(provider.getImageUri().toString());
cv.put(DBHelper.DB_IMAGEPATH, myFile.getAbsolutePath());
At this point the path to the image looks like:
/content/media/.. and everything works ok.
When the app resumes I read the path from database and convert it to Uri:
File tempFile = new File(cursor.getString(imagePathColIndex);
mUri = (Uri.fromFile(mFile));
Now the path in new mUri looks like:
File:///content/... And there is a IONotFound exception.
You should try with
File tempFile = new File(cursor.getString(imagePathColIndex));
mUri = Uri.parse(tempFile.getAbsolutePath());
Testing with the following code
String path = "/content/media";
Uri uri = Uri.fromFile(new File(path));
Log.e(TAG, uri.toString()); //print file:///content/media
uri = Uri.parse(path);
Log.e(TAG, uri.toString()); //print /content/media
EDIT
To read the image can you try
BitmapFactory.decodeStream(new FileInputStream(Uri.parse(path)));
(I have read a lot of similar questions, but bear with me here)
I need to send an image from one Activity (custom camera acitvity), where the second Activity is to upload the image to Picasa Web Album via Google API.
Every example I've found goes something like this:
File f = new File(cacheDir, "image_name.jpg");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);
This works perfectly fine when I use the standard Android Picasa upload activity (or any other sharing app). I can also upload photos via the Picasa example app I am using, when sharing the image from gallery/camera etc.
But I cannot figure out how to build an Intent which uses a "content://---" uri and pass this to another application (neither for this example app or the Picasa standard app)...
Specificially: How can I create an Intent which is compatible with the code below (i.e. uses "content://" uri instead of "file://" uri)?
static class SendData {
String fileName;
Uri uri;
String contentType;
long contentLength;
SendData(Intent intent, ContentResolver contentResolver) {
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = this.uri = (Uri) extras.get(Intent.EXTRA_STREAM);
String scheme = uri.getScheme();
if (scheme.equals("content")) {
Cursor cursor = contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
this.fileName = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
this.contentType = intent.getType();
this.contentLength = cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media.SIZE));
}
}
}
}
Retrieving the File-information from a File uri manually leads to NullPointerException with the Google Http Request used in the app.
Hardcoding the Content uri works. E.g:
uploadIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://media/external/images/media/11"));
Information related to Media files is stored by the MediaStore, which is a ContentProvider (http://developer.android.com/reference/android/provider/MediaStore.Images.html)
MediaStore.Images.DATA column corresponds to the file path and MediaStore.Images._ID column corresponds to the ID.
You need to query for the ID corresponding to your file path and then create a ContentUri out of it (which will be MediaStore.Images.Media.EXTERNAL_CONTENT_URI + id if the image is on the external storage, I'll try to think of a better way to translate the ID into a Content Uri).
http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#query(android.content.ContentResolver, android.net.Uri, java.lang.String[]
I am developing a sort of media player for android. The question is how can i get the cover art of audio file on android.
For example the default android media player shows album covers when listing albums, how can i get this artworks.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.
You can use
MediaMetadataRetriever
class and get track info i.e. Title,Artist,Album,Image
Bitmap GetImage(String filepath) //filepath is path of music file
{
Bitmap image;
MediaMetadataRetriever mData=new MediaMetadataRetriever();
mData.setDataSource(filePath);
try{
byte art[]=mData.getEmbeddedPicture();
image=BitmapFactory.decodeByteArray(art, 0, art.length);
}
catch(Exception e)
{
image=null;
}
return image;
}
I don't know why everybody is making it so complicated you can use Glide to achieve this in simplest and efficient way with just 1 line of code
Declare this path in App Constants -
final public static Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Get Image Uri using Album ID
Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
listOfAlbums.get(position).getAlbumID());
Now simply display album art using uri :-
Glide.with(context).load(uri).placeholder(R.drawable.art_default).error(R.drawable.art_default)
.crossFade().centerCrop().into(holder.albumImage);
Glide will handle caching, scaling and lazy loading of images for you.
Hope it help somebody.
Here i can attach one function that is return album art from media store .
Here in function we just have to pass the album_id which we get from Media store .
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
Based on your comments to others, it seems like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as Nick has suggested, I believe you should be able to retrieve it the way Fudgey suggested.
Android only recognizes files named "AlbumArt.jpg" as Album Covers. Just put the pictures with that name in the album folder and you'll be fine..
I don't know if you read that google is making Stackoverflow the official Android app development Q&A medium but for beginner questions... Now, I know nothing about developing in andriod, but a quick search of the Android Developers site, I found this:
MediaStore.Audio.AlbumColumns
Hopefully it'll help.