除非是一个可以使很多人受益的好问题,否则我很少在这里发帖。我一直在研究Android MMS内容提供商。 我需要访问android MMS数据,而不是图片,而是专门访问视频。
这里有两个很好的例子,第一个问题已有6年历史了,可以回答我的问题,但是代码不起作用(或移植到Python,但是这里没有人喜欢PyJnius或Kivy) How to access android MMS resources such as video/audio etc? How to Read MMS Data in Android?
我已经能够:
我需要帮助:
下面是我在Python中处理的代码,但下面是Java版本。 Python目前无法正常工作。
private static final int RAW_DATA_BLOCK_SIZE = 16384; //Set the block size used to write a ByteArrayOutputStream to byte[]
public static final int ERROR_IO_EXCEPTION = 1;
public static final int ERROR_FILE_NOT_FOUND = 2;
public static byte[] LoadRaw(Context context, Uri uri, int Error){
InputStream inputStream = null;
byte[] ret = new byte[0];
//Open inputStream from the specified URI
try {
inputStream = context.getContentResolver().openInputStream(uri);
//Try read from the InputStream
if(inputStream!=null)
ret = InputStreamToByteArray(inputStream);
}
catch (FileNotFoundException e1) {
Error = ERROR_FILE_NOT_FOUND;
}
catch (IOException e) {
Error = ERROR_IO_EXCEPTION;
}
finally{
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
//Problem on closing stream.
//The return state does not change.
Error = ERROR_IO_EXCEPTION;
}
}
}
//Return
return ret;
}
//Create a byte array from an open inputStream. Read blocks of RAW_DATA_BLOCK_SIZE byte
private static byte[] InputStreamToByteArray(InputStream inputStream) throws IOException{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[RAW_DATA_BLOCK_SIZE];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
我的转换无效,请使用新的Java示例或固定的Python端口!
from kivy.logger import Logger
from jnius import autoclass
from jnius import cast
from android import activity
def InputStreamToByteArray(self, inputStream):
ByteArrayOutputStream = autoclass('java.io.ByteArrayOutputStream')
byteBuffer = ByteArrayOutputStream()
#byte = autoclass('java.lang.Byte') #this is where I'm messing up..
buffer = bytes(1024) #not sure the equivalent in Python
#data = byte[16384]
len = 0
# This just loops forever, len = 4 forever
while int(len) != -1:
Logger.warning(str(len))
len = inputStream.read(buffer)
byteBuffer.write(buffer, 0, len)
return byteBuffer.toByteArray() #wish it worked :(
def LoadRaw(self, uri):
PythonActivity = autoclass('org.renpy.android.PythonActivity')
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
#Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
the_uri = Uri.parse(uri)
inputStream = currentActivity.getContentResolver().openInputStream(the_uri)
# try to read from input stream:
ret = self.InputStreamToByteArray(inputStream)
return ret
data = self.LoadRaw("content://mms/part/2023") #this is a 10 second video
这将帮助很多人。任何帮助表示赞赏!我的代码有任何指导吗?请发布Java示例!