Android: Unable to display thumbnail of video - android

I am trying to display thumbnail of a video but I am not able to generate the thumbnail for the video.
My activity_main contains
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/Thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My Main Activity has the following code
package com.example.video1;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.widget.ImageView;
public class MainActivity extends Activity {
String filePath = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView thumbnail_mini = (ImageView) findViewById(R.id.Thumbnail);
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MINI_KIND);
thumbnail_mini.setImageBitmap(bmThumbnail);
}
}
Its displaying nothing . How can I display the thumbnail of video?

You can't get the thumbnail from a link with the android SDK. You would have to download it locally and then use the thumbnail.utils

Related

Passing an image from an activity to another one

I want to pass images from one activity to another when button clicked. I have one button "Show Image" in first activity. When I click on it, it should pass two images from my mipmap folder of my project and go to second activity and show one of the passed image on the ImageView of that activity. On second activity, I have two buttons which are supposed to receive images and show those images when clicked on each button. I tried using intent to pass the image, however, it didn't work. Is there other way to send images from mipmap folder from one activity to another?
Here is my code:
MainActivity.java
package com.example.abina.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showmyImage();
}
});
}
public void showmyImage(){
Intent intent = new Intent(this, Main2Activity.class);
Bitmap bitmap; // your bitmap
bitmap = null;
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intent.putExtra("byteArray", _bs.toByteArray());
startActivity(intent);
}
}
activity_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="137dp"
android:text="Show Image" />
</RelativeLayout>
Main2Activity.java
package com.example.abina.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
Button image1;
Button image2;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
image1 =(Button) findViewById(R.id.image1);
image2 = (Button) findViewById(R.id.image2);
imageView =(ImageView) findViewById(R.id.imageView);
if(getIntent().hasExtra("byteArray")) {
Bitmap _bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(_bitmap);
}
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<Button
android:id="#+id/image1"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Image1" />
<Button
android:id="#+id/image2"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="0dp"
android:text="Image2" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Images
First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.
Next since they will be in your drawables, I would just pass the #DrawableRes id e.g. the R.id.image_name value.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);
Also I would recommend that you use a public static variable IMAGE_RES_ID_KEY for your extra key to avoid typos.
Then on the other side you can simply
if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}

Glide won't Load images from a URL and crashes the app

So I am having an issue where even the most barebones implementation of Glide to load an image won't work and results in a AppX has stopped. I even created a side app with just the necessary code required to load from a URL and it doesn't work. I have tried several URLs that I know work and get the same issue. I have added the permissions :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
the gradle instructions:
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:23.0.0'
and my activity XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.benhouse.basicglidetest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="138dp"
android:layout_height="495dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout_editor_absoluteX="192dp"
tools:layout_editor_absoluteY="155dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
and my .java for that activity:
package com.example.benhouse.basicglidetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Glide.with(getApplicationContext()).load("https://i.imgur.com/AxETlhd.jpg");
}
}
What is going wrong? I am not getting any stack traces in the logcat or anything and I have isolated the problem as much as possible. Also is there a way to display on the app that the image didn't load?
Try this .
imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(MainActivity.this).load("https://i.imgur.com/AxETlhd.jpg").into(imageView);
Add into(imageView); to the code .
Note
Adding error image and placehoder image will be well .
Glide.with(context).load(url).placeholder(R.mipmap.placeholder).error(R.mipmap.error).into(iv);
Please Check this code:
I had changed some of the lines from your snippet:
package com.example.benhouse.basicglidetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.yourImageView); // get your image view
Glide.with(getApplicationContext())
.load("https://i.imgur.com/AxETlhd.jpg")
.into(imageView); // put image loaded via glide into ImageView
}
}
Add loaded image into imageview
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView) findViewById(R.id.imageView);
Glide.with(EnterPinActivity.this).load("https://i.imgur.com/AxETlhd.jpg").placeholder(R.drawable.plcae_holder).error(R.drawable.plcae_holder).into(imageView);
}
}

Animated Splash screen Android

I want to show my animated gif image in full size on the splash screen, but it's not working. I also used match parent. Whats the problem here? My xml file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.foodie.splash.splashscreen">
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
And this is how it looks:
Java code:
package com.example.foodie.splash;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import com.felipecsl.gifimageview.library.GifImageView;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
public class splashscreen extends AppCompatActivity {
private GifImageView gifImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
gifImageView = (GifImageView)findViewById(R.id.gifImageView);
//Set GIFImageView resource
try{
InputStream inputStream = getAssets().open("foodie.gif");
byte[] bytes = IOUtils.toByteArray(inputStream);
gifImageView.setBytes(bytes);
gifImageView.startAnimation();
}
catch (IOException ex)
{
}
//Wait for 3 seconds and start Activity Main
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
splashscreen.this.startActivity(new Intent(splashscreen.this,MainActivity.class));
splashscreen.this.finish();
}
},5000); // 3000 = 3second
}
}
you choose a wrong scale type, sett the scaleType as FitXY instead of fit center
but of course it lead to change the scales, find out if it has something like centerCrop scale.
<com.felipecsl.gifimageview.library.GifImageView
android:id="#+id/gifImageView"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Grid View Image Error

hello i am new on android. Please help.
My SingleViewActivity java
package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SingleViewActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
// Get intent
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView =(ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.mThumbIds[position]);}}
This singleview xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageViewandroid:id="#+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
So my problem is, when thumb image clicked is not show full image
Sorry my bad english
add scale type to ImageView like this,
android:scaleType="fitXY"

I want to set an image saved on sd card in main layout background through my application

i am creating an application in which i want set different background images in main xml linearlayout.I have stored 5 image files on sd card .now i want to select a pic and set it as my maim xml linearlayout background.so it will replace the previous image and display the new image as background.
First assign an id to the main xml linearlayout, for example in the following case it is named" container"
<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/container">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
Then in the .java code you can find the layout object and set a drawable as its background:
package org.example.app;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String pathName = "/sdcard/gif001.gif";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
}
}
Regards
Ziteng Chen

Categories

Resources