Couldn't Expand RemoteViews - Using Compatible Views, Still Error - android

I've checked the following question, As seen i understood that i can only use compatible views with RemoteViews, i'm using a Linear and ImageView which are working, every time i trigger the Notification method, i get the " Couldn't expand Remoteviews " error .
My layout is as the following :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon2" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon4" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/AppIcon6" />
</LinearLayout>
</LinearLayout>
Sending Notify Code :
private void SendNotifyShortcuts(){
UpdatePreferences();
nBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setOngoing(true);
remoteView = new RemoteViews(getPackageName(), R.layout.notification_layout);
if (App_1 == null) {
remoteView.setImageViewResource(R.id.AppIcon1, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_1, 0);
icon = getPackageManager().getApplicationIcon(App_1);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon1, AppIcon);
}
if (App_2 == null) {
remoteView.setImageViewResource(R.id.AppIcon2, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_2, 0);
icon = getPackageManager().getApplicationIcon(App_2);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon2, AppIcon);
}
if (App_3 == null) {
remoteView.setImageViewResource(R.id.AppIcon3, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_3, 0);
icon = getPackageManager().getApplicationIcon(App_3);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon3, AppIcon);
}
if (App_4 == null) {
remoteView.setImageViewResource(R.id.AppIcon4, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_4, 0);
icon = getPackageManager().getApplicationIcon(App_4);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon4, AppIcon);
}
if (App_5 == null) {
remoteView.setImageViewResource(R.id.AppIcon5, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_5, 0);
icon = getPackageManager().getApplicationIcon(App_5);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon5, AppIcon);
}
if (App_6 == null) {
remoteView.setImageViewResource(R.id.AppIcon6, R.drawable.app_fab);
} else {
Drawable icon = null;
Bitmap AppIcon = null;
final PackageManager pm = getApplicationContext().getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(App_6, 0);
icon = getPackageManager().getApplicationIcon(App_6);
AppIcon = ((BitmapDrawable) icon).getBitmap();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
remoteView.setImageViewBitmap(R.id.AppIcon6, AppIcon);
}
setListeners(remoteView, this);
nBuilder.setContent(remoteView);
nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOFIY_ID, nBuilder.build());
}
public static void CancelNotify(){
try {
nManager.cancel(NOFIY_ID);
} catch (Exception e){
e.printStackTrace();
}
}
public static void setListeners(RemoteViews view, Context context){
Intent App1 = new Intent("App1");
PendingIntent App1P = PendingIntent.getBroadcast(context, 0, App1, 0);
view.setOnClickPendingIntent(R.id.AppIcon1, App1P);
Intent App2 = new Intent("App2");
PendingIntent App2P = PendingIntent.getBroadcast(context, 1, App2, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App2P);
Intent App3 = new Intent("App3");
PendingIntent App3P = PendingIntent.getBroadcast(context, 3, App3, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App3P);
Intent App4 = new Intent("App4");
PendingIntent App4P = PendingIntent.getBroadcast(context, 4, App4, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App4P);
Intent App5 = new Intent("App5");
PendingIntent App5P = PendingIntent.getBroadcast(context, 5, App5, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App5P);
Intent App6 = new Intent("App6");
PendingIntent App6P = PendingIntent.getBroadcast(context, 6, App6, 0);
view.setOnClickPendingIntent(R.id.AppIcon2, App6P);
}
What's the fix ? And i've seen in the questions that i can make a ListView in a notification, What's the way ? as there is no official doc or guide to do it .

Related

Show image from HTML tag in text view?

Xml Layout
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/black" />
Java
txtDescription.setHtml(object.getString("content"), new HtmlHttpImageGetter(txtDescription));
Response from server
{"success":"true","Content":{"id":115,"title":"vvipprogram","content":"<img src=\"https://example.com//media//images//topvvip.jpg" alt=\"topvvip\" width=\"43%\"\/>\r\n"}}
Result Screenshot
I Want to show this image in full page ? How to do this ? plz help me .
You can use this code for set html image and text both from textview:
This is xml code:
<TextView
android:id="#+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/black" />
This is Java code:
public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
private TextView txtDescription;
private Drawable empty;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_condition_use);
txtDescription = (TextView) findViewById(R.id.txtDescription);
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";
Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
txtDescription.setText(spanned);
}
#Override
public Drawable getDrawable(String s) {
LevelListDrawable d = new LevelListDrawable();
empty = getResources().getDrawable(R.drawable.splash1);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(s, d);
return d;
}
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;
#Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
Log.d(TAG, "doInBackground " + source);
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
//mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
mDrawable.setLevel(1);
CharSequence t = txtDescription.getText();
txtDescription.setText(t);
}
}
}

android basic image gallery code error?

i am developing the main activity at the moment that displays multiple images vertically that can be scrolled down. later i would use each image as a thumbnail to a video.
but imageview displays one image only
can someone guide me through it.
Resources res = getResources(); //if you are in an activity
AssetManager am = res.getAssets();
//String temp="";
String fileList[];
try {
fileList = am.list("thumbs");
if (fileList != null)
{
for ( int i = 0;i<fileList.length;i++)
{
Log.d("",fileList[i]);
//temp = temp + fileList[i] + ", ";
if (i == 0)
{
mImage = (ImageView)findViewById(R.id.imageView1);
Drawable d = fetchThumb(fileList[0]);
mImage.setImageDrawable(d);
}
else if(i == 1)
{
mImage = (ImageView)findViewById(R.id.imageView2);
Drawable d = fetchThumb(fileList[1]);
mImage.setImageDrawable(d);
}
else if(i == 2)
{
mImage = (ImageView)findViewById(R.id.imageView3);
Drawable d = fetchThumb(fileList[2]);
mImage.setImageDrawable(d);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Drawable fetchThumb(String filename)
{
try
{
InputStream ims = getAssets().open(filename);
Drawable d = Drawable.createFromStream(ims, null);
return d;
}
catch(IOException ex)
{return null;}
}
xml file to display images:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>

Photoshop image blending mode in Android

After a lot of searching now i am able to create Photoshop blending mode filter in android. Here is my working code for image blending mode in android it is working from android 2.1 to 4.4 enjoy and incase of any query feel free to ask :)
public class MainActivity extends Activity {
Button btnLoadImage1, btnLoadImage2;
TextView textSource1, textSource2;
Button btnProcessing;
ImageView imageResult;
final int RQS_IMAGE1 = 1;
final int RQS_IMAGE2 = 2;
int blendmode=1;
Uri source1, source2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLoadImage1 = (Button) findViewById(R.id.loadimage1);
btnLoadImage2 = (Button) findViewById(R.id.loadimage2);
textSource1 = (TextView) findViewById(R.id.sourceuri1);
textSource2 = (TextView) findViewById(R.id.sourceuri2);
btnProcessing = (Button) findViewById(R.id.processing);
imageResult = (ImageView) findViewById(R.id.result);
/*
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.businnes);
Picture picture = svg.getPicture();
Drawable drawable = svg.createPictureDrawable();
imageResult.setImageDrawable(drawable);*/
btnLoadImage1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE1);
}
});
btnLoadImage2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_IMAGE2);
}
});
btnProcessing.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (source1 != null && source2 != null) {
Bitmap processedBitmap = ProcessingBitmap(blendmode);
blendmode++;
if(blendmode>7)
blendmode=1;
if (processedBitmap != null) {
imageResult.setImageBitmap(processedBitmap);
/*Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_LONG).show();*/
} else {
Toast.makeText(getApplicationContext(),
"Something wrong in processing!",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Select both image!", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RQS_IMAGE1:
source1 = data.getData();
textSource1.setText(source1.toString());
break;
case RQS_IMAGE2:
source2 = data.getData();
textSource2.setText(source2.toString());
break;
}
}
}
#SuppressLint("NewApi")
private Bitmap ProcessingBitmap(int value) {
Bitmap bm1 = null;
Bitmap bm2 = null;
Bitmap newBitmap = null;
try {
bm1 = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(source1));
bm2 = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(source2));
int w;
if (bm1.getWidth() >= bm2.getWidth()) {
w = bm1.getWidth();
} else {
w = bm2.getWidth();
}
int h;
if (bm1.getHeight() >= bm2.getHeight()) {
h = bm1.getHeight();
} else {
h = bm2.getHeight();
}
Config config = bm1.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
newBitmap = Bitmap.createBitmap(w, h, config);
Canvas newCanvas = new Canvas(newBitmap);
newCanvas.drawBitmap(bm1, 0, 0, null);
Paint paint = new Paint();
switch (value) {
case Key.KEYS_BLEND_DARKEN:
Print_Toast("BLEND_DARKEN");
paint.setXfermode(new PorterDuffXfermode(Mode.DARKEN));
break;
case Key.KEYS_BLEND_MULTIPLY:
Print_Toast("BLEND_MULTIPLY");
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
break;
case Key.KEYS_BLEND_ADD:
Print_Toast("BLEND_ADD");
paint.setXfermode(new PorterDuffXfermode(Mode.ADD));
break;
case Key.KEYS_BLEND_DESOLVE:
Print_Toast("BLEND_DESOLVE");
paint.setXfermode(new PorterDuffXfermode(Mode.DST));
break;
case Key.KEYS_BLEND_DESOLVE_LIGHTEN:
Print_Toast("BLEND_LIGHTEN");
paint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
break;
case Key.KEYS_BLEND_DESOLVE_OVERLAY:
Print_Toast("BLEND_OVERLAY");
paint.setXfermode(new PorterDuffXfermode(Mode.OVERLAY));
break;
case Key.KEYS_BLEND_DESOLVE_SCREEN:
Print_Toast("BLEND_SCREEN");
paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
break;
default:
break;
}
paint.setShader(new BitmapShader(bm2, TileMode.CLAMP, TileMode.CLAMP));
/*paint.setAlpha(128);
paint.setDither(true);
paint.setAntiAlias(true);*/
//paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
newCanvas.drawRect(0, 0, bm2.getWidth(), bm2.getHeight(), paint);
//newCanvas.drawBitmap(bm2, 0, 0, paint);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newBitmap;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
void printlog(String tag,String value){
Log.d(tag, value);
}
void Print_Toast(String value){
Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
}
}
Here is Key class
public class Key {
final public static int KEYS_BLEND_DARKEN=1;
final public static int KEYS_BLEND_MULTIPLY=2;;
public static final int KEYS_BLEND_ADD = 3;
public static final int KEYS_BLEND_DESOLVE = 4;
public static final int KEYS_BLEND_DESOLVE_LIGHTEN = 5;
public static final int KEYS_BLEND_DESOLVE_OVERLAY = 6;
public static final int KEYS_BLEND_DESOLVE_SCREEN = 7;
}
xml file is activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/loadimage1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image1" />
<TextView
android:id="#+id/sourceuri1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/loadimage1"
android:layout_below="#+id/loadimage1"
android:layout_marginLeft="62dp"
android:text="TextView" />
<Button
android:id="#+id/loadimage2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/loadimage1"
android:layout_below="#+id/sourceuri1"
android:text="Load Image2" />
<TextView
android:id="#+id/sourceuri2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/sourceuri1"
android:layout_below="#+id/loadimage2"
android:text="TextView" />
<Button
android:id="#+id/processing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/loadimage2"
android:layout_alignRight="#+id/loadimage2"
android:layout_below="#+id/sourceuri2"
android:text="Processing" />
<ImageView
android:id="#+id/result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/processing"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>

Display full screen image in ImageView

I want to display full screen image on another activity after clicking an imageview. I have 6 ImageViews in my layout and each ImageView is getting image from Parse backend. How can I display image on fetching the imagepath ?
public ImageLoader imgl;
ImageView ad1,ad2,ad3,ad4,ad5,ad6;
List<ParseObject> ob;
private ImageView[] imgs = new ImageView[5];
int k=0;
ad1=(ImageView) findViewById(R.id.ad1);
ad2=(ImageView) findViewById(R.id.ad2);
ad3=(ImageView) findViewById(R.id.ad3);
ad4=(ImageView) findViewById(R.id.ad4);
ad5=(ImageView) findViewById(R.id.ad5);
ad6=(ImageView) findViewById(R.id.ad6);
imgs[0] = ad2;
imgs[1] = ad3;
imgs[2] = ad4;
imgs[3] = ad5;
imgs[4] = ad6;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Adverts");
query.orderByDescending("updatedAt");
query.whereEqualTo("Status", true);
try {
ob = query.find();
System.out.println("the urls areeee "+ob);
for (ParseObject country : ob) {
ParseFile image = (ParseFile) country.get("imageFile");
imgl.DisplayImage(image.getUrl(), imgs[k]);
k=k+1;
System.out.println("the urls are"+image.getUrl());
pd.dismiss();
}
} catch (com.parse.ParseException e) {
// TODO Auto-generated catch block
pd.dismiss();
e.printStackTrace();
}
ad1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent ent= new Intent(HomeActivity.this,AdvertsActivity.class);
startActivity(ent);
}
});
}
Set click listener on your ImageView and pass your image url in argument and call method
private void viewImage(String url)
{
final Dialog nagDialog = new Dialog(ProjectDetailActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.dialog_full_image);
ivPreview = (ImageView)nagDialog.findViewById(R.id.imageView1);
BitmapDrawable bmd = (BitmapDrawable)getDrawableFromUrl(url)
Bitmap bitmap = bmd.getBitmap();
ivPreview.setImageBitmap(bitmap);
nagDialog.show();
}
public Drawable getDrawableFromUrl(String imgUrl)
{
if(imgUrl == null || imgUrl.equals(""))
return null;
try
{
URL url = new URL(imgUrl);
InputStream in = url.openStream();
Drawable d = Drawable.createFromStream(in, imgUrl);
return d;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
use xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:layout_gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/hello_world"
android:src="#android:color/white"
android:layout_margin="5dp"
android:scaleType="centerInside"/>
</RelativeLayout>

Android code to record a video using remote IP camera which is accessed by using a url

Here are some links which tell about video recording:
How can I capture a video recording on Android?
https://github.com/churnlabs/android-ffmpeg-sample
and there are also many links which tell about video recording but got no any clue how to use the remote IP camera to record video.
By using different samples on stackoverflow I become able to take picture and save on sdcard but couldn't record video.
If any one has any idea or code along with required files I will be thankful.
For example the url I am using where the IP camera is available is given below:
http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=800x600&amp%3bdummy=1333689998337
Here is the layout code:
<RelativeLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/logo"
android:layout_alignParentTop="true"
android:layout_weight="1" >
<ImageButton
android:id="#+id/btnCam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/camera_icon" >
</ImageButton>
<ImageButton
android:id="#+id/btnVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/camera_icon" >
</ImageButton>
</RelativeLayout>
<LinearLayout
android:id="#+id/LinearLayout03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/LinearLayout01"
android:layout_below="#+id/LinearLayout02"
android:layout_weight="1" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<view
android:id="#+id/mv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
class="com.apps.GrahamConst.MjpegView" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="#drawable/navbar" >
<ImageButton
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/previous" >
</ImageButton>
<ImageButton
android:id="#+id/btnMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/main" >
</ImageButton>
<ImageButton
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/next" >
</ImageButton>
</LinearLayout>
</RelativeLayout>
Here is my Activity
public class CameraDetails2 extends Activity implements OnClickListener {
private MediaScannerConnection m_pScanner;
String drawable = null;
private MjpegView mv;
private ProgressDialog dialog;
private HashMap<String, String> item;
private int id = -1;
private WindowManager winMan;
boolean recording = false;
MediaRecorder recorder;
int bytearraysize = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
winMan = (WindowManager) getApplicationContext().getSystemService(
Context.WINDOW_SERVICE);
if (winMan != null) {
int orientation = winMan.getDefaultDisplay().getOrientation();
if (orientation == 0) {
// Portrait
setContentView(R.layout.cameradetails);
} else if (orientation == 1) {
// Landscape
setContentView(R.layout.cameradetailsl);
}
}
Bundle b = getIntent().getExtras();
ImageButton b1 = (ImageButton) findViewById(R.id.btnNext);
b1.setOnClickListener(this);
ImageButton b2 = (ImageButton) findViewById(R.id.btnMain);
b2.setOnClickListener(this);
ImageButton b3 = (ImageButton) findViewById(R.id.btnPrevious);
b3.setOnClickListener(this);
ImageButton b4 = (ImageButton) findViewById(R.id.btnCam);
b4.setOnClickListener(this);
ImageButton b5 = (ImageButton) findViewById(R.id.btnVideo);
b5.setOnClickListener(this);
id = Integer.valueOf(b.get("id").toString());
item = listarrayadapter.cameraList.get(Integer.valueOf(id));
mv = (MjpegView) findViewById(R.id.mv);
try {
getVal(item.get("cameraLink"));
// getVal("http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg
/video.cgi?resolution=800x600&amp%3bdummy=1333689998337");
} catch (Exception e) {
e.printStackTrace();
mv.setBackgroundResource(R.drawable.offline);
}
}
#Override
protected void onResume() {
// if(recording)
// {
// getVal("http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg
/video.cgi?resolution=800x600&amp%3bdummy=1333689998337");
// }
super.onResume();
}
public void onPause() {
super.onPause();
dialog.dismiss();
mv.stopPlayback();
}
private void getVal(final String url) {
Log.i("URL===", url);
updateButtons();
dialog = ProgressDialog.show(this, null, null, true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = null;
checkUpdate = new Thread() {
public void run() {
mv.setSource(MjpegInputStream.read(url));
mv.setDisplayMode(MjpegView.SIZE_FULLSCREEN);
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int btn = v.getId();
if (btn == R.id.btnMain) {
Intent intent = new Intent();
intent.setClass(CameraDetails2.this, CamerasList.class);
startActivity(intent);
finish();
}
if (btn == R.id.btnNext) {
id += 1;
Intent myintent = new Intent(CameraDetails2.this,
CameraDetails.class);
myintent.putExtra("id", id);
startActivity(myintent);
finish();
}
if (btn == R.id.btnPrevious) {
id -= 1;
Intent myintent = new Intent(CameraDetails2.this,
CameraDetails.class);
myintent.putExtra("id", id);
startActivity(myintent);
finish();
}
if (btn == R.id.btnCam) {
if (mv != null) {
Date dt = new Date();
int years = dt.getYear();
int month = dt.getMonth();
int day = dt.getDay();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
final String filename = years + "" + month + "" + day + ""
+ hours + "" + minutes + "" + seconds;
try {
Bitmap image = MjpegView.savebmp;
File SDCardRoot =
Environment.getExternalStorageDirectory();
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(
SDCardRoot.toString() + "/" +
filename + ".jpg");
BufferedOutputStream bos = new
BufferedOutputStream(
fileOutputStream);
int quality = 95;
image.compress(CompressFormat.JPEG, quality, bos);
final String szFile = SDCardRoot.toString() + "/"
+ filename + ".jpg";
m_pScanner = new MediaScannerConnection(this,
new MediaScannerConnectionClient()
{
public void
onMediaScannerConnected() {
m_pScanner
.scanFile(szFile, null /* mimeType */);
}
public void
onScanCompleted(String path, Uri uri) {
if
(path.equals(szFile)) {
CameraDetails2.this
.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(
getApplicationContext(),
"Image Saved.",
Toast.LENGTH_LONG)
.show();
}
});
m_pScanner.disconnect();
}
}
});
m_pScanner.connect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (btn == R.id.btnVideo) {
if (recording) {
// stop and save
recording = false;
MjpegInputStream.isrecording = false;
List<byte[]> bytelist = new ArrayList<byte[]>();
ArrayList<ByteArrayOutputStream> byteArrayStream =
MjpegInputStream.byteArrayStream;
for (int i = 0; i < byteArrayStream.size(); i++) {
byte[] templist
=byteArrayStream.get(i).toByteArray();
bytelist.add(templist);
}
for (int j = 0; j < bytelist.size(); j++) {
bytearraysize += bytelist.get(j).length;
}
byte[] totalbytes = new byte[bytearraysize];
int f = 0;
for (int j = 0; j < bytelist.size(); j++) {
for (int a = 0; a < bytelist.get(j).length; a++) {
totalbytes[f] = bytelist.get(j)[a];
f++;
}
}
Log.e("num of bytes", "" + totalbytes.length);
// Byte[] bytes = bytelist.toArray(new
//Byte[bytelist.size()]);
try {
writeToFile(totalbytes, "" +
System.currentTimeMillis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
recording = true;
MjpegInputStream.isrecording = true;
// onResume();
// start recording
}
// recorder=new MediaRecorder();
// try {
// recorder.prepare();
// } catch (IllegalStateException e) {
// e.printStackTrace();
// finish();
// } catch (IOException e) {
// e.printStackTrace();
// finish();
// }
//
// //recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// // recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
//
// //
// //
// // CamcorderProfile cpHigh = CamcorderProfile
// // .get(CamcorderProfile.QUALITY_HIGH);
// // recorder.setProfile(cpHigh);
// recorder.setVideoSource(mv.getId());
// recorder.setOutputFile("/sdcard/videocapture_example.mp4");
// recorder.setMaxDuration(50000); // 50 seconds
// recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}
}
public void writeToFile(byte[] bytes, String videoname) throws IOException {
try {
Log.e("num of bytes to be saved", "" + bytes.length);
String path = "/sdcard/" + videoname + ".mp4";
FileOutputStream stream = new FileOutputStream(path);
stream.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void updateButtons() {
ImageButton btnNext = (ImageButton) findViewById(R.id.btnNext);
ImageButton btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
if (id == 0) {
btnPrevious.setEnabled(false);
} else {
btnPrevious.setEnabled(true);
}
if (id == listarrayadapter.cameraList.size() - 1) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
}
}
}

Categories

Resources