【问题标题】:Passing Image from ios/c++ code to JNI Android将图像从 ios/c++ 代码传递到 JNI Android
【发布时间】:2019-10-06 19:53:20
【问题描述】:

我想为用户提供一个功能来保存当前屏幕的屏幕截图。 我正在使用 cocos2d-x v3.0 和 c++,并且是第一次实现此功能。 我做了一些谷歌搜索,找到了这段代码

此代码非常适合在 ios 照片中存储图像

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                  [NSString stringWithUTF8String: filename] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

我认为字节数组可以解决我的问题,但我不知道如何通过 jni 方法/字节数组将此图像传输到 android

更多代码 sn-p

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

std::string str =  CCFileUtils::sharedFileUtils()->getWritablePath();
str.append("/imageNameToSave.png");
const char * c = str.c_str();
tex->saveToFile(c);

this->scheduleOnce(schedule_selector(Dressup_screen_BG_button_view::Photo_Save_Gallery_Android),1.0);

#else

tex->saveToFile("imageNameToSave.png", kCCImageFormatPNG);
 this->scheduleOnce(schedule_selector(Dressup_screen_BG_button_view::Photo_Save_Gallery),1.0);
#endif


// This is native method which will save photo 
-( void )ThisPhotoClick{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *yourArtPath = [documentsDirectory stringByAppendingPathComponent:@"/imageNameToSave.png"];

UIImage *image = [UIImage imageWithContentsOfFile:yourArtPath];

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert !" message:@"Photo Saved To Photos." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];}

安卓方法

public  static void SaveImageAndroidJNI(final boolean visible)
{

    ContextWrapper c = new ContextWrapper(me);
    String path = c.getFilesDir().getPath() + "/imageNameToSave.png";
    System.out.println("Paht to check --"+path);
    File imgFile = new File(path);

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ArrayList<Uri> uris = new ArrayList<Uri>();
    uris.add(Uri.parse(path));

    OutputStream output;
    // Find the SD Card path



    File filepath = Environment.getExternalStorageDirectory();

    // Create a new folder in SD Card
    File dir = new File(filepath.getAbsolutePath()
            + "/Your Folder Name/");
    dir.mkdirs();

    // Create a name for the saved image
    File file = new File(dir, "imageNameToSave.png");

    try {

        output = new FileOutputStream(file);

        // Compress into png format image from 0% - 100%
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.flush();
        output.close();
    }

    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    Intent intent = new Intent();
    Uri pngUri = Uri.fromFile(file);


    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, pngUri);
    intent.setType("image/jpeg");

    me.startActivity(Intent.createChooser(intent, "Share Image"));
}

注意

getWritablePath();由于运行时权限问题,现在无法正常工作,我已经尝试在 android 应用程序启动时请求许可但仍然无法正常工作,所以不建议我这样做

我想将捕获的图像保存在我的安卓设备中,但我找不到方法

My implementation is reference from this question but it's not working

调试

返回 null 作为位图我尝试了几种解码位图的方法,当我将它放在 try 块上时,它显示open failed: ENOENT (No such file or directory)

我观察到 data/data/package 中没有 png,所以我说 getWritable 路径不适用于 android

这是我与 Cocos 社区布道者的conversation

任何帮助都非常明显

【问题讨论】:

    标签: android c++ java-native-interface byte cocos2d-x-3.0


    【解决方案1】:

    对于 Android 端:

    我没有看到任何清单代码,但这是创建路径所必需的。放在&lt;application&gt;标签下&lt;/application&gt;之前:

    <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="com.cognizant.expressprescriptionregistration.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
    

    另外,如果您使用的是 Android SDK 29,请将其添加到您的 Manifest 中并在 &lt;application 标签

    android:requestLegacyExternalStorage="true"
    

    文件夹下 res > layout > xml 添加“file_paths.xml”

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path
            name="any text you want here"
            path="Pictures/"/>
    </paths>
    

    以及用于设置图像存储位置的 Java 代码

    // Create the file where the photo will be stored
        private File createPhotoFile() {
    
            // Name of file
            String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    
            // Location of storage
            File storedDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File photo = null;
    
            try {
    
                // Creates file in storedDir
                photo = File.createTempFile(name, ".jpg", storedDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return photo;
        }
    

    【讨论】:

    • 对不起,我忘了在我的问题上添加这些内容,但我已经尝试过了,我的意思是使用文件提供程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多