Why Can't I Show an Image From Url in Android Studio? - android

For some reason, I can't show image from URL in android studio. Whatever I tried did not happen. First I tried "Picasso", then I tried "Universal Image Loader", then I tried "Firebase Storage" and "Glide". But I couldn't show an image on the internet in any of them. Application works with these 4 methods (Picasso,Universal Image Loader,Firebase Storage), but it does not show the image.
I will only show picasso below so that the problem is not long, but if you want I can show them all. Is there something I have overlooked? Or maybe a few simple settings that I don't know are necessary? so whatever I try, what could be preventing me from showing the image.
Gradle
apply plugin: 'com.android.application'
android { compileSdkVersion 29 buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.deneme.goruntudeneme"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}}
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
}
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/my_image_view"
android:layout_width="350dp"
android:layout_height="350dp"
android:scaleType="centerCrop">
</ImageView></LinearLayout>
Java
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.my_image_view);
Picasso.get().load("https://via.placeholder.com/500").into(imageView);
}}
Permissons
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

When you load a image using picaso then the end url must be end with extension like (.jpg or .png) like this "http://i.imgur.com/DvpvklR.png".
Paste this url then try its working problem is "Your URL is wrong"

As #Ashis said, my problem was solved when I added the following line in the manifest.
android:usesCleartextTraffic="true"

Related

Admob shows only test Ads instead of real Ads

I have developed an app that use AdMob for showing ads. The problem is that test ads are showing instead of real ads in API 24 and above, while work properly in API 23 and below.
I do not know which is the problem but I can assure that :
1. I do not implement .addTestDevice
2. I use my my apps unitID and not the test ID
I attach my onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_text_note);
//Δημιουργία διαφημίσεων banner
//Φόρτωση διαφημίσεων banner
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
and my layout file is :
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:contentDescription="#string/advertisement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-4565************/27********">
</com.google.android.gms.ads.AdView>
my build.gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.sebastian_pa.sebastian_v2"
minSdkVersion 16
targetSdkVersion 27
versionCode 4
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.0.1'
implementation 'com.android.support:design:27.0.1'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-ads:17.0.0'
}
Does anyone knows why this is happening?
There are 3 places that you need to change from your test id to your own id.
In manifest file
In you initialize code
In your xml file
From your post, I can see you only did the last one.
For step 1&2, have a look here https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdk
For step 3 have a look here https://developers.google.com/admob/android/banner?hl=en-US

Unable to use barteksc PDF Viewer in my app

I tried to use [bartecksc AndroidPdfViewer][1]
[1]: https://github.com/barteksc/AndroidPdfViewer in my android app to view PDF files from the assets folder inside the app but it is not working. It shows a blank screen.
Here is what I did:
I added this dependency in the build.gradle file
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
(I even tried using implementation 'com.github.barteksc:android-pdf-viewer:2.8.2')
I added the following widget in activity_main.xml
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I put the following code in the onCreate method
PDFView pdfView=findViewById(R.id.pdfView);
pdfView.fromAsset("sample_file.pdf");
The file that I wanted to display was located in the assets folder with the name sample_file.pdf
Following is my MainActivity.java
package com.umersoftwares.bartekscpdfview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PDFView pdfView=findViewById(R.id.pdfView);
pdfView.fromAsset("sample_file.pdf");
}
}
Following is my activity_main.xml
<?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=".MainActivity">
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.constraint.ConstraintLayout>
My build.gradle file is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.umersoftwares.bartekscpdfview"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
}
The problem was that it was necessary to call the .load() method on the PDFView object to display the pdf. So, in the java file, it should be like this
pdfView.fromAsset("sample_file.pdf")
.load();
Instead of only:
pdfView.fromAsset("sample_file.pdf");
The code you posted is missing necessary permissions for reading storage.
there is a demo over github with the library that you posted. check the code for requesting read storage permission and implement it in your code.
You can refer this sample class of demo provided with library.
https://github.com/barteksc/AndroidPdfViewer/blob/master/sample/src/main/java/com/github/barteksc/sample/PDFViewActivity.java

Android Vector Drawables becoming blurry when resize to lagre scale

I'm trying to use this world map svg, however it is not scaling when I resize the vector to see Singapore. The map become blurry. More specific I set the width and height of imageView to 36000dp.
Here is the width and height of vector drawbles:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="2000dp"
android:height="1001dp"
android:viewportWidth="2000"
android:viewportHeight="1001">
In my Layout file, I already use app:srcCompat instead of src: .
<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=".MainActivity"
android:background="#3A6F5C">
<ImageView
android:id="#+id/Map_svg"
android:layout_width="36000dp"
android:layout_height="36000dp"
app:srcCompat="#drawable/ic_world_map"
/>
</android.support.constraint.ConstraintLayout>
Here is my gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "com.example.alan_lin.flora_shipanimation"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
How can I stop it from becoming blurry when I upscale it? Thank you
I tried the SVG you shared, but it is too complicated to be handled by Android, the app crashes when it tried to load it on runtime saying a STRING_TOO_LARGE int the stack trace.
For your solution, I would suggest using PNGs for these kinds of very complex SVGs.
Two more notes:
If you are using a normal size vector don't forget to set the android:scaleType attribute to your ImageView.
Don't forget to add this line of code in your Application extension:
public class App extends Application {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
...

any View not appearing in the android studio editor

when building the app the view i've put in appears but in the editor it is not appearing, and sometimes when i create a ImageView some of the images I pasted in the drawables causes rendering problems. I recently updated my android studio and now it is full of bugs.the image attached below is a new project, I only added image view and convert it to relative layout since constraint layout gives me a ton of errors. thank you for answering my question.
<?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:visibility="visible"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:textAppearance="#style/TextAppearance.AppCompat"
android:visibility="visible" />
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
P.S.
the image view's src or compatsrc stuff, I've deleted it since it causes errors when building just to test it out. and when opening existing projects the app folder seems to be deleted/moved.
here is the gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.rowel.animation"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
SDK 28 is buggy and has lots of layout issues and program bugs.
you can try adding Base. to your theme's parent in styles.xml
eg:
<resource>
<style name="AppTheme" parent="Theme.AppCompat">
...
to
<resource>
<style name="AppTheme" parent="Base.Theme.AppCompat">
...
if doesn't work, then try Invalidating Cache
if that doesn't work either, try changing targetSdkVersion to 27 or below
Change your compileSdkVersion and targetSdkVersion to 27
and also change this
implementation 'com.android.support:appcompat-v7:27.1.1'

CircularImageView drawble not found

I am using CircularImageView library to show rounded images in my app.
In my xml file,
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.pkmmte.view.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/image"
app:border_color="#EEEEEE"
app:border_width="4dp"
/>
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/image2"/>
</GridLayout>
Android Studio isnt showing #drawable/image. 'image.jpg' exists in my drawable folder. When I hit CTRL+SPACE after drawable/, I get no suggestions.
The weird part is the code android:src="#drawable/image2" in next ImageView works perfectly, gives suggestions,etc.
I've also tried adding those images in CircularImageView's drawable folder.
I've tried cleaning and rebuilding project, doesn't work.
app - > build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.appquest.fitterfox"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile project(':libraries:loadtoast')
compile project(':libraries:circleimageview')
}
Instead of adding your dependencies manually, it's better to fetch them using gradle
compile 'com.pkmmte.view:circularimageview:1.1'
will automatically fetch and add circularimageview to your project

Categories

Resources