Picasso not loading url into ImageView - java

I am using Picasso to load image from url but it doesn't load the image from the url although i made some changes to the url as it contains Arabic characters
MainActivity.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"
android:background="#color/colorAccent"
tools:context="com.alpha25.gridview.HomeActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/imageView"
android:scaleType="centerCrop"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Load Image"
android:layout_centerHorizontal="true"
android:id="#+id/load"/>
and this is my code at mainactivity.java
imageView = (ImageView) findViewById(R.id.imageView);
load = (Button)findViewById(R.id.load);
String url = "https://arabian-chemistry.com/wp-content/uploads/2017/09/%D8%A7%D9%81%D8%AA%D8%B1%D8%A7%D8%B6%D9%8A%D8%A9.png";
load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse(url);
String encodeUriString = Uri.encode(uri.getLastPathSegment());
String uriString = uri.toString().replace(uri.getLastPathSegment(), encodeUriString);
Log.d("Taggggggg", uriString);
Picasso.with(mContext).load(uriString).into(imageView);
}
});

Picasso.with(context)
.load("https://arabian-chemistry.com/wp-content/uploads/2017/09/%D8%A7%D9%81%D8%AA%D8%B1%D8%A7%D8%B6%D9%8A%D8%A9.png")
.into(image);
Try this, Worked for me.

Try this change :-
imageView = (ImageView) findViewById(R.id.imageView);
load = (Button)findViewById(R.id.load);
String url = "https://arabian-chemistry.com/wp-content/uploads/2017/09/%D8%A7%D9%81%D8%AA%D8%B1%D8%A7%D8%B6%D9%8A%D8%A9.png";
load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Picasso.with(MainActivity.this).load(url).into(imageView);

ImageView imageView = (ImageView) findViewById(R.id.imageView);
String url =
"https://arabian-chemistry.com/wp-content/uploads/2017/09/%D8%A7%D9%81%D8%AA%D8%B1%D8%A7%D8%B6%D9%8A%D8%A9.png";
Picasso.with(mContext).load(url).into(imageView);
just these codes in my demo, worked for me.
don't forget permission of INTERNET.

Related

Select a photo from the gallery and show it in another Activity [duplicate]

This question already has answers here:
Select a image from the gallery and show it in another Activity
(3 answers)
Closed 6 months ago.
I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity. The problem is the image wont show to the second activity. Theres no problems running the code. what would be the problem.. PLEASE HELP ME
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonGalleryOpen(View view)
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//noinspection deprecation
startActivityForResult(intent, RESULT_OK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null){
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);
selectedphoto = BitmapFactory.decodeFile(filePath);
cursor.close();
Intent intent = new Intent(MainActivity.this,gallery_view.class);
intent.putExtra("data", selectedphoto);
startActivity(intent);
}
}
}
I am making an mobile app in which i have to select image from gallery by clicking a button and then display it in another activity. The problem is the image wont show to the second activity. Theres no problems running the code. what would be the problem.. PLEASE HELP ME
This is an activity_main.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=".MainActivity">
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_below="#+id/textview1"
android:layout_marginTop="3dp">
<Button
android:id="#+id/bottonGalleryOpen"
android:onClick="buttonGalleryOpen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="pick" />
</RelativeLayout>
</RelativeLayout>
gallery_view.java
public class gallery_view extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_view);
ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
Bitmap selectedImage =(Bitmap)this.getIntent().getParcelableExtra("data");
imageview.setImageBitmap(selectedImage);
}
}
gallery_view.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=".MainActivity">
<RelativeLayout
android:id="#+id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera sample"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<RelativeLayout
android:layout_below="#id/relative_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ImageShow"
android:layout_width="match_parent"
android:layout_height="500dp" />
</RelativeLayout>
</RelativeLayout>
This line in your code does not make sense:
intent.putExtra("data", "selectedphoto");
You are adding here string "selectedphoto" which is in no way connected to selectedphoto variable you initialised earlier. You could put your bitmap to intent extra as byte array but this is in-efficient, especially when the image is large.
Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity.
intent.setData(passed uri here);
In your ShowImage activity do:
URI imageUri = getIntent().getData();

How to hide Linear Layout toolbar for specific webview url?

I have a Toolbar created in Linear Layout associated with a relative layout webview. How to hide this toolbar for specific urls for eg : 'index.php'
My Toolbar code is given below
I have tried many times and cant make it work properly with the given answers so updating the code with the xml layout code.
if (TextUtils.isEmpty(getString(R.string.toolbar))) {
showToolBar = false;
}
if (showToolBar) {
mSearch = (ImageView) findViewById(R.id.search);
mAdd = (ImageView) findViewById(R.id.add);
mProfile= (ImageView) findViewById(R.id.profile);
mHome= (ImageView) findViewById(R.id.home);
mSettings= (ImageView) findViewById(R.id.settings);
// ImageView mRefresh = (ImageView) findViewById(R.id.refresh);
mSettings.setOnClickListener(this);
mHome.setOnClickListener(this);
mProfile.setOnClickListener(this);
mSearch.setOnClickListener(this);
mAdd.setOnClickListener(this);
// mRefresh.setOnClickListener(this);
}
else {
LinearLayout llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
if (llToolbarContainer != null) {
llToolbarContainer.setVisibility(View.GONE);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mAdView.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
}
XML Layout code , please do help
<?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">
<FrameLayout
android:id="#+id/webview_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
tools:context=".universalwebview.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</FrameLayout>
<LinearLayout
android:id="#+id/toolbar_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#fff"
android:orientation="horizontal">
<ImageView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_home"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_search"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_add"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_profile"
android:tint="#color/tintcolor" />
<ImageView
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/ic_action_settings"
android:tint="#color/tintcolor" />
</LinearLayout>
<ImageView
android:id="#+id/image_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="visible"
android:src="#drawable/splash" />
</RelativeLayout>
In Your MainActivity Code you can't make Your Showtoolbar bool variable false by default.As that is the place where your bottom navigation is actually populating.So it may gives a run time error.Instead of that, make your Linear Layout hide when url contains "index.php".
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(url.contains("index.php")
{
findViewById(R.id.toolbar_footer).setVisibility(View.GONE);
}
else
{
findViewById(R.id.toolbar_footer).setVisibility(View.VISIBLE);
}
}
Call this under your WebViewClient .
You need to create a function , that will hide an tool bar. And create custom WebViewClient for WebView
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.contains("index.php")) {
hideToolBar();
}
}
};
Something like this. Not sure that you need onPageFinished, but you can find more info WebViewClient
Get url
Use url.contains("index.php") to judge
set showToolBar = false; or showToolBar = true;
Use if (showToolBar) {} else {}
Try this .
private boolean showToolBar;
private LinearLayout llToolbarContainer;
public void initWebView() {
llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
String url = "your_url";
if (url.contains("index.php")) {
showToolBar = false;
llToolbarContainer.setVisibility(View.GONE);
} else {
showToolBar = true;
llToolbarContainer.setVisibility(View.VISIBLE);
}
mSearch = (ImageView) findViewById(R.id.search);
mAdd = (ImageView) findViewById(R.id.add);
mProfile = (ImageView) findViewById(R.id.profile);
mHome = (ImageView) findViewById(R.id.home);
mSettings = (ImageView) findViewById(R.id.settings);
// ImageView mRefresh = (ImageView) findViewById(R.id.refresh);
mSettings.setOnClickListener(this);
mHome.setOnClickListener(this);
mProfile.setOnClickListener(this);
mSearch.setOnClickListener(this);
mAdd.setOnClickListener(this);
// mRefresh.setOnClickListener(this);
}

Changing ImageView size

I'm trying to have an image show up when the name of it (TextVIew) is clicked. I have the code set up to what I believe is the right thing but the app force closes when I launch it. This is just a test code, so its not my actual app, but the idea is the same so can someone help me figure out why this is crashing?
java file:
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
image.getLayoutParams().height=20;
}
});
}
xml file:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:text="show image" />
You have to have this code (below) inside onCreate()
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
Try it.
Update:
The code could look like this:
public class MainActivity extends Activity {
ImageView image = null;
TextView text = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
image = (ImageView) findViewById(R.id.image);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (image.getVisibility() == View.GONE) {
image.setVisibility(View.VISIBLE);
text.setText("hide the image");
} else {
image.setVisibility(View.GONE);
text.setText("show the image");
}
}
});
}
}
and layout is here:
<LinearLayout 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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:visibility="gone" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show the image" />
</LinearLayout>
Fill your elements in onCreate, not as a field, after setting the content view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
...
And try setting the size of the ImageView not directly from layoutparams but with setHeight/setWidth functions

ImageView.setImageURI() resets layout?

I'm passing the URI of the resource I want to use to the activity that's supposed to display it with a description. Here's the code:
public void onClick(View v) {
final String root = "android.resource://" + getPackageName() + "/";
Intent myIntent = new Intent( v.getContext(), SingleMoveView.class);
myIntent.setData(Uri.parse(root + R.drawable.simby));
myIntent.putExtra("Desc", getResources().getString(R.string.simbus));
startActivity(myIntent);
}
and in the SingleMoveView class:
ImageView view = (ImageView) findViewById(R.id.singleMoveView);
TextView text = (TextView) findViewById(R.id.singleMoveDesc);
Uri path = getIntent().getData();
String desc = getIntent().getStringExtra("Desc");
view.setImageURI(path);
text.setText(desc);
What's happening is I've set up the ImageView and TextView in the XML as such:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#android:style/Theme.Holo.NoActionBar">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/singleMoveView"
android:layout_margin="20dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/singleMoveDesc"
android:layout_margin="20dp"
android:layout_below="#+id/singleMoveView"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
But it's displaying the image in the absolute center of the screen:
Instead of on top w/text underneath. [Note the picture isn't displayed. view.setImageURI(path); was commented out]
Help please? How do I get it to display properly. Thanks! ^_^
Problem solved. It was an issue with the dimensions of the picture. I had to resize it...

How to reference the image from the xml?

This is the oncreate from my application.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
//ImageView img = (ImageView)controlInflater.
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
control.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:gravity="center"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
</LinearLayout>
how to reference the ImageView in control.xml from java code.
View viewControl = controlInflater.inflate(R.layout.control, null);
ImageView img =(ImageView) viewControl.findViewById(R.id.img);
use this to get reference to imageView.
There is a findViewById method in View as well:
ImageView img = (ImageView)viewControl.findViewById(R.id.img)

Categories