H
H
HDDx2019-04-14 00:05:55
Android
HDDx, 2019-04-14 00:05:55

How to use Apache POI to insert a picture into Excel in Android Studio via button?

Hello everyone. A couple of months ago I started learning JAVA from scratch, I still know very little, so don't be surprised if my question turns out to be ordinary for you.
I am writing an application that takes pictures, saves them to the gallery, and after displaying the picture in imageView, transfers the image to the created xls file by pressing a button. Problem: I can't insert an image into Excel even if I enter the path to the image directly. Please help if you know what to do. I do everything as per instructions like ... there are permissions in the manifest for reading and writing.

package com.example.excel2;

import android.graphics.Picture;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Ex();
    }








    public void Ex() {


        try {

            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("FUCK");

//FileInputStream obtains input bytes from the image file
            InputStream inputStream = new FileInputStream("sdcard/Pictures/Ex.jpg");
//Get the contents of an InputStream as a byte[].
            byte[] bytes = IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
            int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
//close the input stream
            inputStream.close();

//Returns an object that handles instantiating concrete classes
            CreationHelper helper = wb.getCreationHelper();

//Creates the top-level drawing patriarch.
            Drawing drawing = sheet.createDrawingPatriarch();

//Create an anchor that is attached to the worksheet
            ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner for the image
            anchor.setCol1(0);
            anchor.setRow1(5);

//Creates a picture

            HSSFPicture pict = (HSSFPicture) drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
            pict.resize();

//Write the Excel file

            FileOutputStream fileOut = new FileOutputStream("sdcard/myFile.xls");
            wb.write(fileOut);
            fileOut.close();


        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.excel2">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

</manifest>

LOGCAT
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.excel2, PID: 13401
    java.lang.NoSuchMethodError: java.util.List.sort
        at org.apache.poi.ddf.EscherDggRecord.sortCluster(EscherDggRecord.java:262)
        at org.apache.poi.ddf.EscherDggRecord.addCluster(EscherDggRecord.java:255)
        at org.apache.poi.ddf.EscherDggRecord.allocateShapeId(EscherDggRecord.java:314)
        at org.apache.poi.hssf.model.DrawingManager2.allocateShapeId(DrawingManager2.java:109)
        at org.apache.poi.hssf.usermodel.HSSFPatriarch.newShapeId(HSSFPatriarch.java:429)
        at org.apache.poi.hssf.usermodel.HSSFPatriarch.afterCreate(HSSFPatriarch.java:153)
        at org.apache.poi.hssf.usermodel.HSSFSheet.getPatriarch(HSSFSheet.java:2112)
        at org.apache.poi.hssf.usermodel.HSSFSheet.createDrawingPatriarch(HSSFSheet.java:2087)
        at org.apache.poi.hssf.usermodel.HSSFSheet.createDrawingPatriarch(HSSFSheet.java:86)
        at com.example.excel2.MainActivity.Ex(MainActivity.java:58)
        at com.example.excel2.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:5411)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)
I/Process: Sending signal. PID: 13401 SIG: 9

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question