PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label android-image. Show all posts
Showing posts with label android-image. Show all posts

Tuesday, July 26, 2022

[FIXED] How to not crop ImageView inside a LinearLayout?

 July 26, 2022     android, android-image, crop     No comments   

Issue

Hi, I'm trying to make something like this

what i want

And this is what I get

enter image description here

This is the xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_marginHorizontal="10dp">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/category_item_background"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:paddingBottom="20dp">

        <ImageView
                android:id="@+id/planetImage"
                android:layout_marginTop="-50dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/earth_transparent"
                />

        <TextView
                android:id="@+id/planetName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:textColor="@color/white"
                android:text="Mars"
                android:fontFamily="@font/assistant_extrabold"
                android:textSize="30dp"/>

        <TextView
                android:id="@+id/planetViews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:fontFamily="@font/assistant_extrabold"
                android:text="155 views"/>

    </LinearLayout>


</LinearLayout>

The image is cropped and I can't figure out how to predict that. I don't care about colour difference right now, I just want to set something, so it doesn't crop the image.


Solution

just put imageview outside

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:background="@drawable/category_item_background"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:paddingBottom="20dp">

        <TextView
            android:id="@+id/planetName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:fontFamily="@font/assistant_extrabold"
            android:gravity="center_horizontal"
            android:text="Mars"
            android:textColor="@color/white"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/planetViews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/assistant_extrabold"
            android:gravity="center_horizontal"
            android:text="155 views" />

    </LinearLayout>

    <ImageView
        android:id="@+id/planetImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:background="@color/black"
        android:src="@drawable/earth_transparent" />

</RelativeLayout>


Answered By - Yura
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 6, 2022

[FIXED] How long can I use the URI from ActivityResultContracts.GetContent() for?

 May 06, 2022     android, android-image, image, registerforactivityresult, uri     No comments   

Issue

Basically, I'm trying to let users choose pictures from their phone's image gallery to use as background images in my app.

Here is my code:

private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
    // e.g. content://com.android.providers.media.documents/document/image%3A31
    android.util.Log.d("dev-", "uri = $uri?.toString()")
    uri?.let {
        // save uri to my DB, etc.
    }
}

My concern is: what happens if the user deletes the images or switches to a new phone. The URI should break, right? And a better approach would be to save the picture to my local app storage and save a URI that points there?

However, I've tried deleting the picture from my phone's gallery, resetting the gallery cache, close & re-open the app, restarting the phone...and I'm still able to the image in my app.

Is it really necessary to create my own copy of the file?

How long does the URI I'm given by registerForActivityResult(ActivityResultContracts.GetContent()) last?

I found this SO post & this article by CommonsWare, but nothing else online and I haven't encountered any errors yet...

Note: I've been testing on Android 12 API 31.


Solution

My concern is: what happens if the user deletes the images or switches to a new phone. The URI should break, right?

Usually it will. More importantly, you may be unable to use that Uri as soon as your app process terminates.

However, I've tried deleting the picture from my phone's gallery, resetting the gallery cache, close & re-open the app, restarting the phone...and I'm still able to the image in my app.

You should not assume that it will work across all Android OS versions, Android device models, gallery apps, etc. You appear to have tested on one device model out of tens of thousands, using an OS version that, at present, is used by a small percentage of the user base.

If, OTOH, you are the only user of your app, and only ever on this device, you are welcome to do whatever you want.

Is it really necessary to create my own copy of the file?

That depends on your objective.

You could use OpenDocument instead of GetContent, and use takePersistableUriPermission() to get long-term access to that content. This is the "attach" action: you want to attach content to your app but you are happy letting external tools manage that content. This handles the process-was-terminated scenario and the phone-was-rebooted scenario. It does not handle the user-nuked-the-content-from-orbit scenario, the user-modified-the-content scenario, etc.

If you need access to the content regardless external user manipulation, make a copy of that content. This is the "import" action: you want a copy of the content to be managed exclusively by your app.



Answered By - CommonsWare
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing