Monthly Archives: August 2017

Share Multiple Images or Files on Android

Yes but you’ll need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND .

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */

ArrayList<Uri> files = new ArrayList<Uri>();

for(String path : filesToSend /* List of the files you want to send */) {
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    files.add(uri);
}

intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);

My Own Solution

    private fun shareSelectedFiles() {

        val files = ArrayList<Uri>()

        CachePupli.selectedMediaList.forEach { s: String ->
            val uri = Uri.parse("file:///$s")
            files.add(uri)
        }

        val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
        intent.setType(FileHelper().getMimeType(files.get(0).path))
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files)

        val clipData = ClipData.newUri(contentResolver, "Share", files.get(0));

        if (files.size > 1) {
            var i = 1

            while (i < files.size) {
                clipData.addItem(ClipData.Item(files.get(i)))
                i++
            }
        }

        intent.clipData = clipData
        startActivityForResult(Intent.createChooser(intent, "Share"), 10001)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        // after sharing multiple files
        if (requestCode == 10001) {
            CachePupli.selectMediaMode = false
            EventBus.getDefault().post(ChangeSelectionModeStatusEvent(false))
        }

        super.onActivityResult(requestCode, resultCode, data)
    }

References
https://stackoverflow.com/questions/15577438/how-can-i-share-multiple-files-via-an-intent

Create PopupMenu with Icons using PopupWindow

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:layoutDirection="rtl"
    android:orientation="vertical"
    android:textDirection="rtl">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingEnd="40dp"
        android:paddingTop="10dp">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="4dp"
            app:srcCompat="@drawable/ic_share_gray_24dp" />


        <android.support.v7.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical"
            android:text="@string/share" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingEnd="40dp"
        android:paddingTop="10dp">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="4dp"
            app:srcCompat="@drawable/ic_delete_gray_24dp" />


        <android.support.v7.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical"
            android:text="@string/delete" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingEnd="40dp"
        android:paddingTop="10dp">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="4dp"
            app:srcCompat="@drawable/ic_create_new_folder_gray_24dp" />


        <android.support.v7.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical"
            android:text="@string/move_to_folder" />

    </LinearLayout>

</LinearLayout>
    fun imageButtonMoreVert_OnClick(v: View) {
        val view = layoutInflater.inflate(R.layout.gallery_selection_menu, null)

        val popup = PopupWindow(this)
        popup.contentView = view
        popup.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popup.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popup.setFocusable(true);

        val location = IntArray(2)
        imageButtonMoreVert.getLocationOnScreen(location)
        val p = Point(location.get(0), location.get(1))

        val OFFSET_X = -20
        val OFFSET_Y = 50

        popup.showAtLocation(view, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
    }

References
https://stackoverflow.com/questions/15454995/popupmenu-with-icons
https://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/

Use Android SDK Documentation offline in Android Studio

Find your configuration files

Windows: %USERPROFILE%\.<CONFIGURATION_FOLDER>/
Mac: ~/Library/Preferences/<CONFIGURATION_FOLDER>/
Linux: ~/.<CONFIGURATION_FOLDER>/

File is located at “Android Studio Configuration folder”/config/options/jdk.table.xml

find javadocPath

...
<jdk version="2">
  <name value="Android API 25 Platform" />
  <type value="Android SDK" />
  <homePath value="$USER_HOME$/android-sdk" />
  <roots>
      ...
    <javadocPath>
      <root type="composite">
        <root type="simple" url="http://developer.android.com/reference/" />
      </root>
    </javadocPath>
      ...
  <root type="simple" url="file:///home/mauricio/android-sdk/docs/reference" />
  <!-- if it's Windows -->
  <root type="simple" url="file://C:/android-sdk/docs/reference" />

References
https://stackoverflow.com/questions/42893969/how-to-use-android-sdk-documentation-offline

Cache Images before showing with Glide on Android

    private fun cacheFiles(files: List<MediaFile>?) {
        if (files != null) {

            val measuredWidth = Statics.getScreenWidth() / 4
            //val requestOption = RequestOptions.overrideOf(width, width)

            for (f in files) {
                if (f.mimeType.startsWith("image")) {
                    GlideApp.with(CachePupli.context)
                            .load(File(f.path))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .override(measuredWidth, measuredWidth)
                            .centerCrop()
                            .submit(measuredWidth, measuredWidth)
                }
            }
        }
    }

References
https://github.com/bumptech/glide/wiki/Loading-and-Caching-on-Background-Threads

Working with LruCache on Android

int memClass = ( ( ActivityManager) activity.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass / 8;
LruCache cache = new LruCache<String, Bitmap>( cacheSize );

References
http://www.vogella.com/tutorials/AndroidApplicationOptimization/article.html
https://developer.android.com/reference/android/util/LruCache.html
https://developer.android.com/samples/DisplayingBitmaps/src/com.example.android.displayingbitmaps/util/DiskLruCache.html
https://developer.android.com/topic/performance/graphics/cache-bitmap.html