Access Denied Error

“”""""
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.ActivityResultListener;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.Deleteable;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.File;

@SimpleObject(external = true)
@DesignerComponent(category = ComponentCategory.EXTENSION, description = “Cropping extension created by SHUVO.”, iconName = “http://voltscdn.weebly.com/uploads/2/0/8/1/20817010/idea.png”, nonVisible = true, version = 1)
@UsesPermissions(permissionNames = “android.permission.WRITE_EXTERNAL_STORAGE, android.permission.READ_EXTERNAL_STORAGE”)
public class Cropper_img extends AndroidNonvisibleComponent implements ActivityResultListener, Component, Deleteable {
private static final String LOG_TAG = “Crop”;
public static int aspectX;
public static int aspectY;
public static String fileName;
public static String imagePath;
public final int REQUEST_CODE = this.form.registerForActivityResult(this);
private ComponentContainer container;
private Context context;

Copy to clipboard

public Cropper_img(ComponentContainer container2) {
    super(container2.$form());
    this.container = container2;
    Log.d(LOG_TAG, "Crop Created");
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
}

@SimpleProperty
public void Path(String path) {
    imagePath = "file://" + path;
}

@SimpleFunction
public void DoCrop(String filename) {
    fileName = filename;
    Uri picture = Uri.parse(imagePath);
    File file = new File(Environment.getExternalStorageDirectory(), fileName);
    try {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(picture, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        intent.putExtra("output", Uri.parse("file://" + file.toString()));
        this.container.$context().startActivityForResult(intent, this.REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this.context, "Corresponding activity not found.", 0).show();
    }
}

@SimpleEvent
public void AfterCrop(String path) {
    EventDispatcher.dispatchEvent(this, "AfterCrop", new Object[]{path});
}

public void resultReturned(int requestCode, int resultCode, Intent data) {
    if (requestCode == this.REQUEST_CODE && data != null) {
        AfterCrop("" + Uri.parse(new File(Environment.getExternalStorageDirectory(), fileName).toString()).toString());
    }
}

public void onDelete() {
    this.form.unregisterForActivityResult(this);
}

}
“””"""
I am using this code.
and when I write “filename” to “somethin/myfile.jpg”
then i got an error called access denied. Can any developer help me?

Hi @Shukto_Shuvo Welcome to the Community
I have a question.
What happens when you ask for WRITE permission?

1 Like

it says access denied

Maybe the file path given is invalid, try using full path.