Sharing Bitmap Image and Text android - android

I am having a Framelayout and some TextViews in my application for which I am loading data from server and setting the background of FrameLayout with Image loaded from server using Picasso and setting TextViews in the same manner. But I want to share it using intent and I am unable to figure out how to do that? Do I have to download the Image First?
My Code in AsyncTask:
Picasso.with(ctx).load(myPlace.getImg()).into(new Target() {
#Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
// TODO Auto-generated method stub
pImg.setBackgroundDrawable(new BitmapDrawable(ctx.getResources(), bitmap));
}
#Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "Failed Loading", Toast.LENGTH_SHORT).show();
}
});
pname.setText(myPlace.getName());
pdes.setText(myPlace.getDescription());
Share Button:
Button shareBtn = (Button) findViewById(R.id.sharebtn);

As Picasso will cache the image, you wont need to re download it in another activity(as long as it is on the same application). Add these to your code and see if it helps
steps one and two required only if myPlace is local to AsyncTask
Step 1: make AsyncTask return a MyPlace object
private class ClassName extends AsyncTask<..., ..., MyPlace> {
...
}
Step 2: return myPlace object from onPostExecute
Step 3: pack values in bundle and send over intent
Intent intent = new Intent(this, NextActivity.class)
Bundle bundle = new Bundle();
bundle.putString(IMG_URL, myPlace.getImg()); // assuming getImg returns string of url
// put name and desc in the same way
intent.addExtra(bundle);
startActivity(intent);
Step 4: get values in new activity
Intent intent = getIntent();
String url = intent.getExtra().getString(IMG_URL);
// others in the smae way

Related

How to load JPEG as a BITMAP from server using Picasso

I've tried using the method:
bmp = (Bitmap) Picasso.with(Feed.this).load(IMAGE_URL+loadImageUrl).resize(width, width).get();
But I get a "Picasso DownloadResponseException" even when I take out the (Bitmap) typeCast
I've also tried:
Picasso.with(Feed.this).load(IMAGE_URL+image).resize(width, width).into(target);
With
private Target target = new Target() {
#Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
Log.d("FAILED", "Bitmap Failed");
}
#Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void onBitmapLoaded(Bitmap bmp, LoadedFrom arg1) {
RoundedCornersDrawable drawable = new RoundedCornersDrawable(getResources(), bmp);
theImage.setImageDrawable(drawable);
}
};
But it gives me nothing, the image doesn't load it just stays blank and the log message under the BitmapFailed method does not come up... The images stored on my server are of type "jpg".
Please help me
Figured it out. Turns out I had the wrong URL. Also make sure you Don't call that Picasso.get() method inside the UI thread...

Contacts are listing in one row of listview?

I make an app in which phone contact show in listview. but i want to convert it into widget. ie, I want to show contact list on the homescreen in a listview. I searched on google and found some example but none worked. any one has any link. I am not posting my code because it not worked. any help will be appriciated
Update :-
I am able to show list on homescreen. how to bind this listview to contact list
I tried to add this method in my viewfactory class
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
ids = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
aa.add(new ContactStock(info.name,info.phone));
}
phone.close();
//Collections.sort(aa);
adapt=new ContactListAdapter(MainActivity.this, aa);
//listcontact.setAdapter(new ContactListAdapter(MainActivity.this, aa));
//adapter = new ArrayAdapter<Info>(this, android.R.layout.simple_list_item_1);
listcontact.setAdapter(adapt);
}
But it doesn't work, it is working in my app but not in homescreen widget
Update 2 :-
I am showing contact list in widget but all contacts are showing in one row.My remote view class is
public class DialerViewFactory implements RemoteViewsFactory {
private static final int mCount = 2;
private List<Info> mWidgetItems = new ArrayList<Info>();
private Context mContext;
private int mAppWidgetId;
public DialerViewFactory(Context context,Intent intent){
mContext=context;
mAppWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
#Override
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
//ContentResolver cr=mContext.getContentResolver();
//getnumber(cr);
RemoteViews rv=new RemoteViews(mContext.getPackageName(),R.layout.row);
rv.setTextViewText(R.id.textrow1, mWidgetItems.toString());
rv.setTextViewText(R.id.textrow2, "Kya");
Bundle extras = new Bundle();
extras.putInt(DialerWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.textrow1, fillInIntent);
return rv;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
ContentResolver cr1=mContext.getContentResolver();
getnumber(cr1);
}
#Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
}
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
mWidgetItems.add(info);
}
phone.close();
}
class Info {
public String name;
public String phone;
public String picture;
#Override
public String toString() {
return name;
}
}
}
What you are looking for is AppWidgets using Collections. This can accept a ListView as the widget for presenting your data.
This will basically consist of
an AppWidgetProvider, as in any widget
a RemoteViewsService / RemoteViewsFactory, responsible for
producing the views in your ListView, much like an Adapter would do.
Since you are not sharing your code, it's hard to say what is not working for you, but you can find working examples in the sdk samples. Note that the samples have apparently been removed from latest sdk versions, so you should probably download the samples for older sdk's, if I remember correctly. You can also browse the source here.
EDIT :
adapt=new ContactListAdapter(MainActivity.this, aa);
listcontact.setAdapter(adapt);
This will not work in app-widgets, for 2 reasons :
You don't have an Activity available. For widgets, you can use the Context provided as argument of the onUpdate callback in your AppWidgetProvider, although in your case it probably won't be needed.
You don't instantiate an Adapter for app-widgets. To bind data to your ListView on your homescreen, you call setRemoteAdapter on the main RemoteView inside your AppWidgetProvider (in the onUpdate method), like this :
Example :
// Setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Creating the main RemoteView and binding data to it.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rv.setRemoteAdapter(R.id.listview, intent);
Basically, the RemoteViewsFactory's getViewAt method acts much like the getView in an Adapter would, you just have to copy and adapt the code of your adapter to make it work in the RemoteViewsFactory. The sample of StackWidget contains a RemoteViewsFactory where all the method are commented to let you know where to perform which tasks.
EDIT 2 :
#Override
public int getCount() {
// This method tells the factory how many rows it has to produce
// you should be returning the size of your data collection
// so in your case :
return mWidgetItems.size();
}
Also, in you getViewAt method you are setting the text as the toString() of the array (this is why you have all contacts in one row), whereas you should read the fields of the individual Info object for the position given by the method:
// get the current Info object by position :
Info currentInfo = mWidgetItems.get(position);
// then use that object to fill each row, like in getView for an Adapter, for example :
rv.setTextViewText(R.id.textrow1, currentInfo.phone);
rv.setTextViewText(R.id.textrow2, currentInfo.name);

how to retrieve Data from Intent ?? [duplicate]

This question already has answers here:
How do I get extra data from intent on Android?
(16 answers)
Closed 8 years ago.
I just want to show an simple Employee record on next layoutPage/Activity !!
this is my EmpLogin Java File
public class EmpLogin extends Activity {
private Button show;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// TODO Auto-generated method stub
show=(Button)findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText no=(EditText)findViewById(R.id.getno);
EditText name=(EditText)findViewById(R.id.getname);
EditText sal=(EditText)findViewById(R.id.getsalary);
Intent emp = new Intent(getApplicationContext(),EmpShow.class);
emp.putExtra("EmpNO",(no.getText().toString()));
emp.putExtra("EmpName",(name.getText().toString()));
emp.putExtra("Sal",(sal.getText().toString()));
startActivity(emp);
}
});
}
}
How to use Retrieve data from the Intent ??? By using getExtra() method ??? or there is simple way ?? this my EmpShow.class file !!
public class EmpShow extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
// TODO Auto-generated method stub
Intent show = getIntent();
}
}
As you passed data insideintent, So try to fetch the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
If your extra data is represented as strings, then you can use
intent.getStringExtra(String name) method.
As per your OP's class and intent. Here is the snipped which can fetch the values from your intent against respective variable,
public class EmpShow extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.empshow);
Intent intent = getIntent();
String empNo = intent.getStringExtra("EmpNO");
String empNname = intent.getStringExtra("EmpName");
String empSal = intent.getStringExtra("sal");
}
}
First Activity:
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("VAL",value1);
startActivity(i);
Second Activity:
Intent getI=getIntent();
String a =getI.getStringExtra("VAL");

AsyncTaskLoader incorrect method call onLoadFinished

I'm trying to deal with AsyncTaskLoader. I implemented the Interface LoaderCallbacks follows:
public class MainActivity extends Activity implements LoaderCallbacks<String> {
...
...
#Override
public Loader<String> onCreateLoader(int id, Bundle args) {
Log.w("loader","onCreate");
return new JSONLoader(this);
}
#Override
public void onLoadFinished(Loader<String> arg0, String arg1) {
Log.w("loader","finish");
}
#Override
public void onLoaderReset(Loader<String> arg0) {
Log.w("loader","onReset");
// TODO Auto-generated method stub
}
public void useLoader() {
Log.w("loader","useLoader");
Bundle args = new Bundle();
// ...
// fill in args
// ...
Loader loader =
this.getLoaderManager().initLoader(0, args, this);
Log.w("HashLoader",String.valueOf(loader.hashCode()));
// with support library:
// Loader loader =
// context2.getSupportLoaderManager().initLoader(0, args, this);
// call forceLoad() to start processing
loader.forceLoad();
}
I get a hung event for button:
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
useLoader();
}
});
When I push the button the first time the method onLoadFinished() is called after the loader. But when push button a second time it calls the first onLoadFinished().
Logcat :
useLoader
onCreate
1087415440
good
finish
useLoader
finish
1087415440
good
A little about AsyncTaskLoader:
public class JSONLoader extends AsyncTaskLoader<String> {
public JSONLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
public String loadInBackground() {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.w("loader","good");
return " ";}
Why does the method onLoadFinished() not wait for the end of the Thread?
I think the initLoader function doesn't destroy and create a new loader. then on the second run, your custom loader has already been 'loaded' => it calls onLoadFinish()
try to destroy it and recreate it simply by replacing the initLoader call by a restartLoader
( Reference )
Loader loader = this.getLoaderManager().restartLoader(0, args, this);

strange error on my android set frame application

i create an application that set a frame to a image bitmap... but when the process give a result, i don't know, why the result bitmap are also saved to my original bitmap variabel, this is my code :
main code :
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.frame);
tampilFrame = (ImageView)findViewById(R.id.ivframe);
oribmp = temporary.getBitmap();
temp = oribmp;
tampilFrame.setImageBitmap(temp);
frame00 = (ImageView)findViewById(R.id.frs00);
frame00.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr00));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame01 = (ImageView)findViewById(R.id.frs01);
frame01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr01));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame02 = (ImageView)findViewById(R.id.frs02);
frame02.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr02));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame03 = (ImageView)findViewById(R.id.frs03);
frame03.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr03));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
}
}
the variabel frame00, frame01, frame02 are ImageView that I set as a Button. When the user click the frame image, it will load an image from drawable and stored in a variabel named "frame" and also call a asyncTask named "backtask".
this is the "backtask" code
public class backtask extends AsyncTask<Void, Void, Void> {
ProgressDialog prog;
#Override
protected void onPreExecute(){
super.onPreExecute();
prog = ProgressDialog.show(FrameActivity.this, "", "Progress...");
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
temp = setFrame(oribmp);
Log.i("temp", "widht : "+String.valueOf(temp.getWidth())+" Height : "+String.valueOf(temp.getHeight()));
return null;
}
#Override
protected void onPostExecute(Void res){
super.onPostExecute(res);
tampilFrame.setImageBitmap(temp);
prog.dismiss();
Log.i("setFrame", "Complete");
}
}
this backtask code i used to make a loading progress when the application runs the "setFrame()" code. and this is my "setFrame" code :
public Bitmap setFrame(Bitmap bit){
for (int i=0;i<frame.getWidth();i++){
for(int j=0;j<frame.getHeight();j++){
if (Color.alpha(frame.getPixel(i, j))!=0){
bit.setPixel(i, j, frame.getPixel(i, j));
}
}
}
frame.recycle();
return bit;
}
the oribmp is a variabel that containt original bitmap get from class temporary.getBitmap()
when the "temp = setFrame(oribmp);" finish, the ori bmp has also changes same as the temp.
when i checked to class temporary, the bitmap variabel that contain original bitmap on the temporary class also changes to the result of the setFrame...
this is the code pf temporary class
public class temporary {
private static Bitmap Mbmp;
public static Bitmap getBitmap () {
return Mbmp;
}
public static void setBitmap(Bitmap oribimp) {
// TODO Auto-generated method stub
Mbmp = oribimp;
Log.i("editor", "simpen bitmap");
}
}
temporary class is a class that i used to send the bitmap value to another activity in my application.. this is what the FrameActivity looks like on the run..
strart activity
this is the look of FrameActivity when its first start, the original bitmap is that gecko
set frame 1
this is the looks when set the first frame
set frame 2
this is the looks when set the second frame, cause the oribmp has the same value like the result of the first seFrame, on the second setFrame they looks like pile up
im sory if my question is too long, maybe anyone can give me solution :D

Categories

Resources