I created an app working with saving a screenshot to the USB storage. But I cannot open that image from USB storage. Please help me to solve this issue. This image I don't want to open and keep it in the ImageView but I want to load with the image viewer or another app like we open from Gallery.
<?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:id="#+id/main"
tools:context=".MainActivity">
<Button
android:id="#+id/btn"
android:text="Take Screen"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#color/colorPrimary"
android:scaleType="fitCenter" />
<Button
android:id="#+id/btnLoad"
android:text="Take Screen"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java
btn_load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// My image name "restaurant.jpg"
// image keep in USB storage\restaurant.jpg
}
});
Glide is an open source library that can able to show an image from any source. You can use this in the following steps.
Add this is build.gradle file:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
And you MainActivity:
//Initialize ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);
btn_load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Loading image from any file location into imageView
String photoPath = Environment.getExternalStorageDirectory()+"/restaurent.jpg"; // need to add storage read permission
Glide.with(MainActivity.this)
.load(new File(photoPath))
.into(imageView);
}
});
Hope this will help.
// Now this is how to open PDF file from storage or external App
// Open from Storage File
public void openFileFromStorage(){
File file = new File("/sdcard/testpdf/test.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
// Open External App. How to find Package name install "System Info" in Play Store.
public void openAppExternal(){
Intent intent = getPackageManager().getLaunchIntentForPackage("com.adobe.reader");
startActivity(intent);
}
// Open External Website
public static Intent openApp(Context context){
try {
context.getPackageManager().getPackageInfo("https://www.geeksforgeeks.org/file-class-in-java/",0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));
}catch (Exception e){
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));
}
}
Related
picasso and glide are not loading the image from web. Below is the jave and xml. First i use picasso which is commented out now, then i use the glide library, but the problem is same. nothing appears on screen. if i ad image source from assets it appears on preview of android studio but i want to load image from web and show it in the image view. please correct my mistake if any..? Thanks.
JAVA:
ImageView imageBanner1 = (ImageView) findViewById(R.id.imageBanner1);
ImageView imageBanne2 = (ImageView) findViewById(R.id.imageBanner2);
Glide.with(getApplicationContext()).load("http://example.com/appBanner1.jpg")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageBannerl);
/*Picasso.with(getApplicationContext())
.load("http://example.com/appBanner1.jpg")
.skipMemoryCache()
.into(imageBannerl);*/
imageBanner1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent inttv=new Intent(MainActivity.this,Play.class);
inttv.putExtra("url","http://example.com");
startActivity(inttv);
}
});
Glide.with(getApplicationContext()).load("http://example.com/appBanner2.jpg")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageBanner2);
/*Picasso.with(getApplicationContext())
.load("http://example.com/appBanner2.jpg")
.skipMemoryCache()
.into(imageBanner2);*/
imageBannerAd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent imageBannerBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(imageBannerBrowserIntent);
}
});
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/tabHost"
android:layout_centerHorizontal="true"
android:id="#+id/imageBannerLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageBanner1"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:background="#color/app_background" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageBanner2"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:background="#color/app_background" />
</LinearLayout>
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("url").into(imageView);
I think you need to check your ImageView variable name.
You have used ImageView imageBanner1 and ImageView imageBanne2 as variable name but never used it.Instead you have used imageBannerChannel and imageBannerAd as variable name. Once Check your code again. And make sure your url is fine.
I have searched a lot but I got samples to fetch thumbnail from an Url or from Http,
but I actually need to get thumbnail of an image from my own directory on sdcard withot accesing the Android default thumbnail directory, thats is I need to fetch a thumbnail of an image from
mnt/sdcard/myown/1.png
Can anybody help for this please?
Thanks in advance.
Environment.getExternalStorageDirectory() returns "/mnt/sdcard"
String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
#Override
public void onCreate(Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
image = (ImageView) findViewById(R.id.imageView1);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
String imagePath = Environment.getExternalStorageDirectory().toString() +"/myown/1.png";
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
image.setImageBitmap(bitmap);
break;
}
}
};
your layout.xml
<?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="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
</LinearLayout>
I would like to write a text in my activity like
Buy my app
I need to make this text clickable. So once one click on it, it opens the Market app and my app shows up.
What is the appropriate view? How can I add the web link?
Would you please help (provide a small code)?
Use this code to achieve your goal.
public void onCreate(Bundle request)
{
super.onCreate(request);
setContentView(R.layout.market);
Button buy = (Button)findViewById(R.id.btnBuyMyApp);
buy.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnBuyMyApp:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=your package name"));
startActivity(intent);
break;
}
}
In xml you can use this
<?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="fill_parent"
android:orientation="vertical"
android:paddingTop="5dip" >
<Button
android:id="#+id/btnBuyMyApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:padding="6dip"
android:text="Buy"
android:textColor="#F8B334"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
I hope it helps
Write following line of code in onClick() method
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=YourPackageName"));
startActivity(intent);
This will open default AndroidMarket application in android mobile and shows your application
You can simply use a TextView in your layout xml file with the text "But my app".
Find that TextView in the android code using the findViewById(). Then set a onClickListener() on the TextView. In the onClickListener() use the following code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.rovio.angrybirds&hl=en")););
I have just used the angry birds app example, instead of that you can give the link of your own app.
Please use below code.
TextView myWebSite = (TextView)findViewById(R.id.myWebSite)
myWebSite.setText("Link is:- " + "market://details?id=your package name");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);
And please see below link for more information.
Android – Creating links using linkify
Check this:
<TextView
android:id="#+id/click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Market"/>
in Activity:
TextView text=(TextView) findViewById(R.id.click);
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com"));
startActivity(intent);
}
});
Dont forget to include internet permission in manifest file.
I am able to read pdf file, but I have one problem
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/sdcard/Test.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PDFTest.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
Am using the above code, when I click Button it takes me to another view where PDF content is visible. I dont want to use two activities. But I want to see the pdf content on same view. How to do that. How to stop navigating to next view.
My XML file is:
`<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="Button"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
`
Can anyone help me on this context.
You need to use a Library not an Intent.
Try the open source library muPDF
is it possible to open an picture, which is stored in the res/drawable folder with the Android build in Image Viewer? I don't get it work. I tried the follwoing way
Button ButtonPhase2 = (Button) findViewById(R.id.button1);
ButtonPhase2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
myIntent.setData(Uri.parse("android.resource://de.test.app/" + R.drawable.testbild));
myIntent.setType("image/png");
startActivityForResult(myIntent, 1);
}
});
But I get the Error:
Unable to start Activity ComponentInfo{com.android.gallery/com.android.camera.ViewImage}: java.lang.nullPointerException
I think it is the wrong path?
ya you can do i by this way:
first you need to declare your layout in xml
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:orientation="horizontal">
<ImageButton android:id="#+id/anim_btn_play"
android:layout_width="155dp" android:layout_height="80dp"
android:layout_marginLeft="260dp" android:layout_marginTop="150dp"
>
</ImageButton>
</LinearLayout>
Java file:
ImageButton tns_imgv_back;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_and_show);
{
tns_imgv_back= (ImageButton)findViewById(R.id.anim_btn_play)
tns_imgv_back.setImageResource(R.drawable.icon);
anim_btn_play11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// you can do any thing over here when you click it will fire
}
});
}}